RouteCollection.php第219行中的MethodNotAllowedHttpException

zlo*_*tte 15 php laravel laravel-5.1

当我存储帖子时,我收到此错误

MethodNotAllowedHttpException in RouteCollection.php line 219:
Run Code Online (Sandbox Code Playgroud)

什么可以导致这个问题?

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');
Run Code Online (Sandbox Code Playgroud)

我正在使用Laravel 5.1,我不知道这一天...

Jef*_*ert 9

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

{!! Form::open(['method' => 'patch']) !!}
Run Code Online (Sandbox Code Playgroud)

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

<input name="_method" type="hidden" value="PATCH">
Run Code Online (Sandbox Code Playgroud)

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

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

Route::post('posts/store-new-post', 'PostsController@store');
Route::post('posts/{slug}', 'PostsController@update');
Run Code Online (Sandbox Code Playgroud)