wobsoriano
wobsoriano

Reputation: 13462

RESTful API using Laravel with AngularJS

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

Answers (3)

shashankgaurav
shashankgaurav

Reputation: 65

Basically when you are following the second method, you are separating your frontend and backend. Following are the advantages of this method -

  • You can focus entirely on logic when you are writing APIs and and when you are working on frontend, you can focus completely on UI without worrying about the backend logic.
  • And If you decide to make an mobile app for website, then you won't have to write the backend logic separately. You can use the same APIs for both mobile app and web app.
  • If you decide to do some changes in backend logic. It will be much easier to update the website if you have both UI and Logic seperated.

Laravel and AngularJS go pretty well together. Good Luck :)

Upvotes: 1

Kalhan.Toress
Kalhan.Toress

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

zedfoxus
zedfoxus

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

Related Questions