简体   繁体   中英

MethodNotAllowedHttpException in RouteCollection.php line 219

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..

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');

尝试添加到你的模型: protected $guarded = ['_token'];

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!

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.

Check your form tag

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

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

在我的情况下,最后有一个额外的“/”,如:POST / api / clients /我已经删除它并且已经工作:POST / api / clients

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM