问题描述
当我存储帖子时出现此错误
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', 'AuthAuthController@getLogin'); Route::post('auth/login', 'AuthAuthController@postLogin'); Route::get('auth/logout', 'AuthAuthController@getLogout'); // Registration routes... Route::get('auth/register', 'AuthAuthController@getRegister'); Route::post('auth/register', 'AuthAuthController@postRegister');
我正在使用 Laravel 5.1,但我一天都想不通..
I'm using Laravel 5.1 and I can't figure this out for a day..
推荐答案
由于您将帖子更新的方法设置为 patch,请确保您打开您的表单以使用该方法:
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']) !!}
如果你没有使用 Form 类,你也可以确保有一个 隐藏在表单下面的名为 _method 的元素:
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">
同样,如果您通过 AJAX 发送此数据,只需在通过 POST 发送请求之前将 _method 键添加到设置为 'PATCH' 的有效负载中.某些浏览器(IE 7/8)不支持通过 XMLHttpRequest 的 PATCH HTTP
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
您的另一个选择是更改您的路由以接受 POST 数据:
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');