RouteCollection.php第218:4行中的MethodNotAllowedHttpException [英] MethodNotAllowedHttpException in RouteCollection.php line 218:4

查看:152
本文介绍了RouteCollection.php第218:4行中的MethodNotAllowedHttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel中提交表单时,我将获得MethodNotAllowedHttpException

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

Route::post('cards/{card}/notes','NotesController@store');

NotesController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;

class NotesController extends Controller
{
    public function store()
    {
        return request()->all();
    }
}

解决方案

确保您没有路线,例如说Route::post,其参数位于您要点击的路线的前面.

例如:

Route::post('{something}', 'SomethingController@index');
Route::post('cards/{card}/notes', 'NotesController@store');

在这种情况下,无论您尝试发送到卡片路线的什么内容,它都会始终命中something路线,因为{something}会截取cards作为有效参数并触发SomethingController. 将something路线放在纸牌路线下方,它应该可以工作.

I will get MethodNotAllowedHttpException when submitting a form in laravel

Html file

<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

Route::post('cards/{card}/notes','NotesController@store');

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.

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. Put the something route below the cards route and it should work.

这篇关于RouteCollection.php第218:4行中的MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆