我正在尝试学习Laravel,并且正在遵循一系列名为laracast的教程。我在episode 24,“提交PUT请求的表单。简而言之,虽然表单方法设置为POST,但标记使用隐藏值将方法设置为PUT。不过,当我这样做时,我从标题中收到错误消息:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD.
从教程中,我希望POST也是受支持的方法。但是,当我尝试解决此问题时,我可以找到的所有资源仅告诉我我已经知道的信息。不支持PUT,但是我可以伪造/覆盖它,然后它们指的是我已经完成的操作...还有其他原因导致我收到此错误消息吗?
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]);
}
答案 0 :(得分:4)
您忘记了表单中的ID,这应该可以解决您的问题:
action="/competition-categories/{{$competitionCategory->id}}"
答案 1 :(得分:0)
最常见的情况是缓存。当您添加新路线或更改路线中的某些内容时,请始终在php artisan optimize
之后运行以刷新缓存。
答案 2 :(得分:0)
我建议使用命名路由以获取更多信息和消息,请参阅=>