Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Route::resource() GET & POST work, but PUT & DELETE throw MethodNotAllowedHttpException

I'm writing a webservice API (in laravel 4.2).
For some reason, the routing to one of my controllers is selectively failing based on HTTP method.

My routes.php looks like:

Route::group(array('prefix' => 'v2'), 
    function()
    {
        Route::resource('foo', 'FooController',
            [ 'except' => ['edit', 'create'] ]
            );
        Route::resource('foo.bar', 'FooBarController',
            [ 'except' => ['show', 'edit', 'create'] ]
            );
    }
);

So, when I try any of GET / POST / PUT / PATCH / DELETE methods for the
project.dev/v2/foo or project.dev/v2/foo/1234 urls, everything works perfectly.

But, for some reason, only GET and POST work for project.dev/v2/foo/1234/bar. The other methods just throw a 405 (MethodNotAllowedHttpException).
(fyi, I am issuing requests via the Advanced Rest Client Chrome extension.)

What's going on?
What am I missing?

like image 720
mOrloff Avatar asked Nov 19 '14 23:11

mOrloff


People also ask

What does route :: resource do in Laravel?

Route::resource: The Route::resource method is a RESTful Controller that generates all the basic routes required for an application and can be easily handled using the controller class.

How do I name a resource route in Laravel?

x | Laravel 5. x). There are two ways you can modify the route names generated by a resource controller: Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.

What is resource in Laravel?

Laravel Resources are another layer added on top of Laravel Eloquent models. It allows creating a uniform format or shapes for the data returned for these Eloquent models.


1 Answers

For the ones who are using Laravel versions > 4.2 use this :

php artisan route:list

This will give the list of routes set in your application. Check if routes for PUT and DELETE are allowed in your routes or not. 405 error is mostly because there is no route for these methods.

like image 163
stackMonk Avatar answered Oct 04 '22 02:10

stackMonk