[英]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: 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.. 我正在使用Laravel 5.1,我不知道这一天...
Since you're setting the method on the post's update to be patch
, be sure you open your form to use that method: 由于您要将帖子更新的方法设置为patch
,请确保打开表单以使用该方法:
{!! 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: 如果您没有使用Form
类,您还可以确保在窗体下面有一个名为_method
的隐藏元素 :
<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. 同样,如果您通过AJAX发送此数据,只需在通过POST发送请求之前将_method
密钥添加到设置为'PATCH'
的有效负载中。 Some browsers ( IE 7/8 ) do not support PATCH HTTP through XMLHttpRequest 某些浏览器( 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');
尝试添加到你的模型: 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. 这适用于GET请求,但我会将我的表单设置为自己提交 - 即。 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). 我没有在我的表单上指定一个操作 - 这意味着如果我在上/posts
它工作(因为我为该路由设置了适当的POST端点),但是从主页/
它总是会给我您描述的MethodNotAllowedHttpException(因为没有为此设置POST数据路由)。 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. ...虽然我猜在表单上明确设置一个动作(或为/
设置一个POST路由)也会完成这项工作。
I'm new to Laravel, so there might well be other approaches that are better than either of the above! 我是Laravel的新手,所以可能还有其他方法比上述任何方法都要好!
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()方法中添加所需的路由方法(POST,GET)。
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. 在这里“ / path / ”应该是“ / path ”,最后不要使用“ / ”。
在我的情况下,最后有一个额外的“/”,如:POST / api / clients /我已经删除它并且已经工作:POST / api / clients
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.