Kousha
Kousha

Reputation: 36199

Using Laravel (or other PHP MVC) and AngularJS

I am making an app and I was wondering what is a better way to approach this. I want to use Laravel as the backend, and AngularJS as the frontend.

Should I then leave the entire rendering and View completely to AngularJS and only use Laravel through AJAX calls? Or should I still load the basics from Laravel, and use AngularJS to enhance the experience?

Upvotes: 1

Views: 140

Answers (1)

brunocascio
brunocascio

Reputation: 1986

Yes. You could use Laravel as RESTFul API and consume from client without using blade (so if you want it)

Here example:

class PostController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
         return Post::orderBy('created_at', 'desc');
    }
}

service in angular: ( I use Restangular lib, but you can use $http)

app.factory("PostsService", function(Restangular) {
  return {
    /*
    * GET /posts -> Trae todos los posts
    */
    all: function() {
      return Restangular.all('posts').getList();
    }
  }
});

controller angular:

app.controller('HomeController', function($scope, PostsService){
  PostsService.all().then(function(posts){
    $scope.posts = posts.data;
  });

});

Upvotes: 1

Related Questions