Paulo Calvo
Paulo Calvo

Reputation: 81

Laravel/Angular API integration

I have built an API in laravel. One of the routes it responds to is:

/api/categories/[category_id]

I am working on integrating this with an Angular frontend with a factory so I am doing this:

apiServices.factory('apiService', ['$resource',
  function($resource){
    return $resource('api/categories', {}, {
      'get': {method:'GET', params:{category_id:'@category_id'}, isArray:false}
    });
  }]);

The get method however returns an URL with a query string like this:

/api/categories?category_id=[category_id]

How can I modify it so that it follows Laravel's API and creates and URL like this:

/api/categories/[category_id]

I would prefer to avoid modifying the API to respond to query strings if possible although I could eventually go that route if changing the factory method is too hard.

Upvotes: 0

Views: 286

Answers (1)

Ashrith
Ashrith

Reputation: 725

This will work. category_id needs to be a route param instead of query parameter.

apiServices.factory('apiService', ['$resource',
function($resource){
return $resource('api/categories/:category_id', {category_id:'@category_id'}, {
  'get': {method:'GET', isArray:false}
});
}]);

Upvotes: 2

Related Questions