[英]MethodNotAllowedHttpException in RouteCollection.php line 218:4
I will get MethodNotAllowedHttpException when submitting a form in laravel 在Laravel中提交表单时,我将获得MethodNotAllowedHttpException
Html file HTML文件
<form method="POST" action="/cards/{{$card->id}}/notes">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<textarea name="body" class="form-control"></textarea>
<button type="submit">Add Note</button>
</form>
routes.php routes.php文件
Route::post('cards/{card}/notes','NotesController@store');
NotesController.php NotesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store()
{
return request()->all();
}
}
Make sure you don't have a route, say a Route::post
with a parameter that lies in front of the route you are trying to hit. 确保您没有路线,例如说一个Route::post
,其参数位于您要点击的路线的前面。
For example: 例如:
Route::post('{something}', 'SomethingController@index');
Route::post('cards/{card}/notes', 'NotesController@store');
In this case, no matter what you try to send to the cards route, it will always hit the something
route because {something}
is intercepting cards
as a valid parameter and triggers the SomethingController
. 在这种情况下,无论您尝试将什么发送到纸牌路线,它都将始终命中something
条路线,因为{something}
会将cards
作为有效参数进行拦截并触发SomethingController
。 Put the something
route below the cards route and it should work. 将something
路线放在纸牌路线下方,它应该可以工作。
MethodNotAllowedHttpException
is thrown when no matching route (method and URI) was found, but a route with a matching URI but not matching method was found. 如果找不到匹配的路由(方法和URI),但是找到了具有匹配的URI但没有匹配方法的路由,则抛出MethodNotAllowedHttpException
。
In your case, I guess the issue is because URI parameters differ between the route and the controller. 在您的情况下,我想这是因为路由和控制器之间的URI参数不同。
Here are two alternatives you can try: 您可以尝试以下两种选择:
Route::post('cards/notes','NotesController@store');
public function store($card) { return request()->all(); }
I have tried to solve this error in lumen and it took me quite a lot of time to figure out the problem. 我试图解决流明中的这个错误,花了我很多时间才能解决这个问题。 The problem is with laravel itself. 问题出在laravel本身。
Sometimes if you have another route like GET device/{variable}, laravel stops in this first route... 有时,如果您有另一条路线,例如GET device / {variable},laravel会在第一条路线中停下...
So what you need to do is change the route POST device
to POST device/add
因此,您需要做的是将路由POST device
更改为POST device/add
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.