15
votes

When I storing a post I get this error

MethodNotAllowedHttpException in RouteCollection.php line 219:

What can cause this problem ??

Routes.php:

Route::get('home', 'PostsController@index');
Route::get('/', 'PostsController@index');
Route::get('index', 'PostsController@index');

Route::get('posts', 'PostsController@index');
Route::get('post/{slug}/{id}', 'PostsController@show');
Route::get('posts/sukurti-nauja-straipsni', 'PostsController@create');
Route::patch('posts/store-new-post', 'PostsController@store');
Route::get('post/{slug}/{id}/edit', 'PostsController@edit');
Route::patch('posts/{slug}', 'PostsController@update');


Route::get('tags/{tags}', 'TagsController@show');
Route::get('categories/{categories}', 'CategoriesController@show');

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

I'm using Laravel 5.1 and I can't figure this out for a day..

6
This happens when you are sending something by post and trying to fetch it by get or the other way around.patricio

6 Answers

9
votes

Since you're setting the method on the post's update to be patch, be sure you open your form to use that method:

{!! Form::open(['method' => 'patch']) !!}

If you're not using the Form class, you can also just ensure there's a hidden element called _method underneath the form:

<input name="_method" type="hidden" value="PATCH">

Similarly, if you're sending this data via AJAX, just add a _method key to the payload set to 'PATCH' before sending the request via POST. Some browsers (IE 7/8) do not support PATCH HTTP through XMLHttpRequest

Your other option is to change your route to accept POST data instead:

Route::post('posts/store-new-post', 'PostsController@store');
Route::post('posts/{slug}', 'PostsController@update');
1
votes

Check your form tag

<form action="/path/" method="post">

in here "/path/" should be "/path" , do not use "/" at the end.

1
votes

Try adding to You Model: protected $guarded = ['_token'];

1
votes

In my case there was a extra "/" at the end, something like: POST /api/clients/ I've deleted it and has worked: POST /api/clients

0
votes

I was having this problem too, but in my case it turned out to be due to having those multiple routes set up to the same controller action:

Route::get('/',     'PostsController@index');
Route::get('posts', 'PostsController@index');

This worked fine for GET requests, but I'd set my form to submit to itself – ie. I hadn't specified an action on my form – which meant that if I was on /posts it worked (since I'd set up an appropriate POST endpoint for that route), but from the home page / it would always give me the MethodNotAllowedHttpException that you describe (because there was no POST data route set up for that). It took ages to figure out why the form seemed to sometimes work and sometimes not.

In the end I fixed it by changing the route for / into a redirect, like this:

Route::get('/', function(){
    return redirect('posts');
});

...although I guess explicitly setting an action on the form (or setting a POST route for / too) would have done the job too.

I'm new to Laravel, so there might well be other approaches that are better than either of the above!

0
votes

Navigate to vendor/laravel/framework/src/Illuminate/Foundation/Middleware/VerifyCsrfToken.php and add route method you want(POST,GET) within function isReading()method.

Hope this may help someone.