此路由不支持 PUT 方法。支持的方法:GET、HEAD

The PUT method is not supported for this route. Supported methods: GET, HEAD

我正在努力学习 Laravel,我正在学习一系列名为 laracast 的教程。我在 episode 24,“提交 PUT 请求的表单。简短的故事是标记使用隐藏值将方法设置为 PUT,尽管表单方法设置为 POST。不过,当我这样做时,我从标题中得到错误信息:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 
The PUT method is not supported for this route. Supported methods: GET, HEAD.

根据教程,我希望 POST 也是一种受支持的方法。然而,当我尝试解决这个问题时,我能找到的所有资源都只是告诉我我已经知道的。不支持 PUT,但我可以伪造 it/override 它,然后他们会参考我已经完成的操作...还有其他原因导致我收到此错误消息吗?

HTML 形式:

 <form method="POST" action="/competition-categories">
                @csrf
                @method('PUT')

                <div class="form-group row">
                    <label for="competition-category-name-input" class="col-4 col-form-label">Name</label>
                    <div class="col-8">
                        <input id="competition-category-name-input" name="competition-category-name-input" type="text" class="form-control" required="required" value="{{ $competitionCategory->name }}">
                    </div>
                </div>
                <div class="form-group row">
                    <label for="competition-category-abbreviation-input" class="col-4 col-form-label">Abbreviation</label>
                    <div class="col-8">
                        <input id="competition-category-abbreviation-input" name="competition-category-abbreviation-input" type="text" class="form-control" required="required" value="{{ $competitionCategory->abbreviation }}">
                    </div>
                </div>
                <div class="form-group row">
                    <div class="offset-4 col-8">
                        <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </div>
            </form>

web.php 片段:

//Competition Categories
Route::get('/competition-categories', 'CompetitionCategoryController@index');
Route::get('/competition-categories/create', 'CompetitionCategoryController@create');
Route::get('/competition-categories/{competitionCategory}', 'CompetitionCategoryController@show');
Route::get('/competition-categories/{competitionCategory}/edit', 'CompetitionCategoryController@edit');
Route::post('/competition-categories/{competitionCategory}', 'CompetitionCategoryController@store');
Route::put('/competition-categories/{competitionCategory}', 'CompetitionCategoryController@udpate');
Route::delete('/competition-categories/{competitionCategory}', 'CompetitionCategoryController@destroy');

来自控制器的代码段:

public function update(Request $request, CompetitionCategory $competitionCategory)
{
    $competitionCategory->update($this->validateCompetitionCategory());

    return redirect()->route('competition-categories' , [$competitionCategory]);
}

最常发生这种情况的是您的缓存。当您添加新路线或更改路线中的某些内容时,总是 运行 在 php artisan optimize 之后刷新您的缓存。

您忘记了表单中的 ID,这应该可以解决您的问题:

action="/competition-categories/{{$competitionCategory->id}}"

我建议使用命名路由以获取更多信息和消息,请参阅 =>

https://laravel.com/docs/7.x/routing#named-routes