How can I globally configure Laravel Route :: resource for ng.resource compatibility (Laravel route for each ng resource)?

Laravl 5 uses put/patch

verbs to update a resource, while Angular uses ng.resource post

by default to create and update. How do I globally set Laravel Route::resource

to track Angular behavior (Laravel route for each ng resource)?

(It is possible to make Angular compatible with Laraval as well, but I'm not sure which approach is better.)

+3


source to share


2 answers


I am not aware of laravel REST capabilities. But still I suggest changing the behavior of Angular.

PUT

The PUT implementation is pretty straightforward.

You can change the behavior of the ng resource when creating a factory with $ resource (url, parameters, actions), the third parameter describes custom actions ...

The https://docs.angularjs.org/api/ngResource/service/ $ resource provides an example of creating a PUT method that will be available update

for both a service and $update

for example.



app.factory('Notes', function($resource) {
return $resource('/notes/:id', null,
    {
        'update': { method:'PUT' }
    });
}]);
// In our controller we get the ID from the URL using ngRoute and $routeParams
// We pass in $routeParams and our Notes factory along with $scope
app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes',
                                   function($scope, $routeParams, Notes) {
// First get a note object from the factory
var note = Notes.get({ id:$routeParams.id });
$id = note.id;

// Now call update passing in the ID first then the object you are updating
Notes.update({ id:$id }, note);
// This will PUT /notes/ID with the note object in the request payload
});

      

PATCH

In theory it is also possible to create the PATCH behavior described here - Partial Updates (aka PATCH) using a $ resource based service

But I wouldn't do it with ng resource. You can do a lot by specifying the transformRequest or transformResponse function (I'm using this for Java Spring REST API usage). But still ng resource doesn't support PATCH on its own, so if you need I would try another layer for REST.

+3


source


This is wrong, you can also use PUT / PATCH with angle characters, please read about this on the $ http page

Labels

Keyboard shortcuts are also available. All shortcut methods require passing in the url and a data request must be passed for POST / PUT requests.

$http.get('/someUrl').then(successCallback);
$http.post('/someUrl', data).then(successCallback);

      



Full list of quick access methods:

$http.get
$http.head
$http.post
$http.put
$http.delete
$http.jsonp
$http.patch

      

-1


source







All Articles