問題描述
當(dāng)我存儲帖子時出現(xiàn)此錯誤
When I storing a post I get this error
MethodNotAllowedHttpException in RouteCollection.php line 219:
什么會導(dǎo)致這個問題??
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..
推薦答案
由于您將帖子更新的方法設(shè)置為 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 發(fā)送此數(shù)據(jù),只需在通過 POST 發(fā)送請求之前將 _method
鍵添加到設(shè)置為 'PATCH'
的有效負(fù)載中.某些瀏覽器(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 數(shù)據(jù):
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');
這篇關(guān)于RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!