Reputation: 13462
I'm trying to figure out something. Most of the time, the way I create a RESTful API is by doing something like this:
Route:
Route::get('/news', 'NewsController@show');
Controller:
class NewsController extends Controller
{
public function show()
{
$news= News::all();
return view('newsview', compact('news'));
}
}
Blade:
@foreach ($news as $n)
<li> {{$n->title}} {{$n->author}}</li>
@endforeach
And now, I'm trying to learn AngularJS
. The way I see some tutorials on the web is they are using AngularJS
as front-end
and Laravel
as the back-end
. And now I see something like this:
Route:
Route::get('/news', 'NewsController@show');
Controller:
class NewsController extends Controller
{
public function show()
{
return News::all();
}
}
HTML/JS:
<div ng-app="myApp" ng-controller="newsCtrl">
<ul>
<li ng-repeat="x in news">
{{ x.title + ', ' + x.author }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('newsCtrl', function($scope, $http) {
$http.get("/news")
.success(function(response) {$scope.news = response.records;});
});
</script>
What's the advantage of using one to another? I'm really interested in using AngularJS
.
Upvotes: 3
Views: 1455
Reputation: 65
Basically when you are following the second method, you are separating your frontend and backend. Following are the advantages of this method -
Laravel and AngularJS go pretty well together. Good Luck :)
Upvotes: 1
Reputation: 21901
Yeah the idea is so good, because mainly you can maintain your htmls
and backend
separately (can host fronend and backend seperate servers) so that you can only focus on what you doing html
or laravel
at one single time. And the other thing is server that backend hosted is getting less traffic and processing, because angular will do some of the processing in the browser (if your not using API laravel needs to process the blade files you can reduce that using API because databinding to html is done in the browser side) and server performances remains high.
And you can maintain the SPA
(single page application) which is great for UX
. so that users will love that.
and there are couple of libraries that are written to laravel to make the REST API
here is one of the popular one. DINGO
Upvotes: 1
Reputation: 37119
Using AngularJS could assist you in creating a relatively richer UI with single page applications (SPA) that a user may find, perhaps, more responsive. With using only Laravel/PHP, your client will notice page reloads. With AngularJS, the user may not be required to leave the current page. That is a slight advantage in my opinion.
On the flip side, using AngularJS would require you to learn AngularJS. It appears you are already interested, so learning will be fun. AngularJS may have somewhat of a steeper learning curve and then you will be maintaining Laravel and AngularJS vendor codebases (upgrades, I mean). So that is a slight disadvantage.
Using AngularJS, you may be tempted to utilize a strategy of using Laravel or any framework that just provides RESTful interfaces and allow AngularJS to take care of all UI rendering. If you later decide to remove dependency on PHP and use Python, sure enough you can - just allow Python to serve the RESTful endpoints.
Upvotes: 1