RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32785670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:11:26  来源:igfitidea点击:

MethodNotAllowedHttpException in RouteCollection.php line 219

phplaravellaravel-5.1

提问by zlotte

When I storing a post I get this error

当我存储帖子时出现此错误

MethodNotAllowedHttpException in RouteCollection.php line 219:

What can cause this problem ??

什么会导致这个问题?

Routes.php:

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

我正在使用 Laravel 5.1,但我一天都想不通。

采纳答案by Jeff Lambert

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

由于您将帖子更新的方法设置为patch,请确保打开表单以使用该方法:

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

If you're not using the Formclass, you can also just ensure there's a hidden element called _methodunderneath the form:

如果您不使用Form该类,您还可以确保在表单下方有一个隐藏元素_method

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

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

同样,如果您通过 AJAX 发送此数据,只需在通过 POST 发送请求之前向_method设置为的有效负载添加一个键'PATCH'。某些浏览器 ( IE 7/8) 不支持通过 XMLHttpRequest 的 PATCH HTTP

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

您的另一个选择是更改您的路线以接受 POST 数据:

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

回答by Ahmet U?ur

Check your form tag

检查您的表单标签

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

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

在这里“ /path/”应该是“ /path”,最后不要使用“ /”。

回答by PawelW

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

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

回答by Efrain Toribio Reyes

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

就我而言,最后有一个额外的“/”,例如: POST /api/clients/ 我已删除它并已工作: POST /api/clients

回答by Nick F

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

这适用于 GET 请求,但我将我的表单设置为提交给自己 - 即。我没有在我的表单上指定一个动作——这意味着如果我在/posts它上面工作(因为我已经为那个路由设置了一个合适的 POST 端点),但是从主页/它总是会给我 MethodNotAllowedHttpException 你描述(因为没有为此设置 POST 数据路由)。花了很长时间才弄清楚为什么这种形式有时似乎有效,有时却无效。

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.

...虽然我想在表单上明确设置一个操作(或设置一个 POST 路由/)也可以完成这项工作。

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

我是 Laravel 的新手,所以很可能还有其他方法比上述任何一种方法都更好!

回答by srivat1

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

导航到 vendor/laravel/framework/src/Illuminate/Foundation/Middleware/VerifyCsrfToken.php 并在函数 isReading()method 中添加您想要的路由方法(POST,GET)。

Hope this may help someone.

希望这可以帮助某人。