2015-09-25 66 views
13

當我存儲後我得到這個錯誤MethodNotAllowedHttpException在RouteCollection.php線219

MethodNotAllowedHttpException in RouteCollection.php line 219: 

什麼可能導致此問題?

routes.php文件:

Route::get('home', '[email protected]'); 
Route::get('/', '[email protected]'); 
Route::get('index', '[email protected]'); 

Route::get('posts', '[email protected]'); 
Route::get('post/{slug}/{id}', '[email protected]'); 
Route::get('posts/sukurti-nauja-straipsni', '[email protected]'); 
Route::patch('posts/store-new-post', '[email protected]'); 
Route::get('post/{slug}/{id}/edit', '[email protected]'); 
Route::patch('posts/{slug}', '[email protected]'); 


Route::get('tags/{tags}', '[email protected]'); 
Route::get('categories/{categories}', '[email protected]'); 

// Authentication routes... 
Route::get('auth/login', 'Auth\[email protected]'); 
Route::post('auth/login', 'Auth\[email protected]'); 
Route::get('auth/logout', 'Auth\[email protected]'); 

// Registration routes... 
Route::get('auth/register', 'Auth\[email protected]'); 
Route::post('auth/register', 'Auth\[email protected]'); 

我使用Laravel 5.1,我不明白這一點了一天..

+0

當你發送郵寄東西,並試圖把它牽獲取或反過來會出現這種情況。 – patricio

回答

8

既然你對這篇文章的更新方法設置爲是patch,請確保您open your form使用該方法:如果你不使用Form

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

,你也可以保證有一個hidden element called _method形式下:

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

同樣,如果您是通過AJAX發送這個數據,只是通過POST發送請求之前添加_method鍵值設置爲'PATCH'有效載荷。有些瀏覽器(IE 7/8)不支持PATCH HTTP通過XMLHttpRequest的

你的另一種選擇就是改變你的路線,接受POST數據,而不是:

Route::post('posts/store-new-post', '[email protected]'); 
Route::post('posts/{slug}', '[email protected]'); 
+0

我面臨着我的服務器的相同問題。但在我的本地服務器上工作正常。使用L5.1我沒有使用通過ajax使用post方法的補丁。任何幫助 –

+0

這個答案只是讓我意識到,有時我只是一個愚蠢的人。哈哈哈!我使用POST,但在我的路線中,我忘了使用Route :: post()而不是Route :: get()。 – Jonjie

0

我太有這個問題,但在我的情況下,它變成出是由於那些具有多個路由設置爲相同的控制器動作:

Route::get('/',  '[email protected]'); 
Route::get('posts', '[email protected]'); 

這工作得很好GET請求,但我把我的形式提交給自己–即。我沒有在我的表單–上指定一個動作,這意味着如果我在/posts上工作(因爲我爲該路由設置了適當的POST端點),但是從主頁/它總是會給我MethodNotAllowedHttpException你所描述的(因爲沒有爲此設置POST數據路由)。花費了很長時間才弄清楚爲什麼表格有時會起作用,有時甚至不起作用。

最後我固定它通過改變路線/成重定向,像這樣:

Route::get('/', function(){ 
    return redirect('posts'); 
}); 

...雖然我猜明確設置窗體上的動作(或設置POST路線/)也可以完成這項工作。

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

0

導航到vendor/laravel/framework/src/Illuminate/Foundation/Middleware/VerifyCsrfToken.php並在函數isReading()方法中添加所需的路由方法(POST,GET)。

希望這可以幫助某人。

+0

這是laravel的一部分,你不想擺弄它。 Laravel 5.3將這個文件移動到另一個位置(vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php)。 –

0

檢查你的表單標籤

<form action="/path/" method="post"> 
在這裏

/路/」 應爲 「/路徑」,不要使用 「/」 結尾。

1

嘗試加入您型號: protected $guarded = ['_token'];

相關問題