Laravel中POST请求触发MethodNotAllowedHttpException问题求助
Laravel POST请求更新数据库遇MethodNotAllowedHttpException问题排查
问题场景
Laravel项目中使用POST请求更新数据库时触发MethodNotAllowedHttpException错误,具体情况:
ItemController包含常规update方法与自定义updateQuantityIncrement方法- 表单设置为POST请求,且携带
_method="put"隐藏参数 - 已在资源路由上方定义自定义方法的POST路由,也尝试过
match方式配置路由,但报错依旧
相关代码
自定义updateQuantityIncrement方法
public function updateQuantityIncrement(Request $request, Item $item){ //save data Log::info('This is some useful information.'); $itemUpdate = Item::where('id',$item->id)->update([ 'name' => $item->name, 'articleNo' => $item->articleNo, 'source' => $item->source, 'menge' => $item->menge + 1, 'img_path' => $item->img_path, ]); if($itemUpdate){ return redirect()->route('items.show', ['item' => $item]) ->with('success', 'Artikel erfolgreich Aktualisiert!'); } return back()->withInput(); }
常规update方法
public function update(Request $request, Item $item){ //save data $itemUpdate = Item::where('id',$item->id)->update([ 'name' => $request->input('name'), 'articleNo' => $request->input('articleNo'), 'source' => $request->input('source'), 'menge' => $request->input('menge'), 'img_path' => $request->input('img_path'), ]); if($itemUpdate){ return redirect()->route('items.show',['item'=>$item])->with('success','Artikel erfolgreich Aktualisiert!'); } return back()->withInput(); }
表单代码
<form method="POST" action="{{ route('items.updateQuantityIncrement',[$item->id]) }}"> {{ csrf_field() }} <input type="hidden" name="_method" value="put"> <div class="row"> <div class="col-md-4"> <button class="btn btn-primary" type="submit" value="submit">+</button> </div> </div><br><br><br> </form>
原路由配置
Route::post('items/{item}/updateQuantityIncrement', 'ItemController@updateQuantityIncrement') ->name('items.updateQuantityIncrement'); Route::resource('items', 'ItemController');
尝试过的路由配置
Route::match(['get', 'post'], 'items/{item}/updateQuantityIncrement', [ 'as' => 'items.updateQuantityIncrement', 'uses' => 'ItemController@updateQuantityIncrement']);
报错信息
MethodNotAllowedHttpException in RouteCollection.php (line 255) at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php (line 242) at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php (line 176)
问题原因及解决方案
核心原因
表单中的_method="put"会将实际的POST请求伪装成PUT请求,但你的路由仅配置了POST(或包含POST的match规则),服务器收到PUT请求后找不到匹配的路由,因此触发方法不允许的异常。
解决方案
有两种处理方式可选:
移除表单的
_method隐藏参数
既然路由配置的是POST方法,直接发送原生POST请求即可:<form method="POST" action="{{ route('items.updateQuantityIncrement',[$item->id]) }}"> {{ csrf_field() }} <!-- 移除伪造PUT请求的隐藏参数 --> <div class="row"> <div class="col-md-4"> <button class="btn btn-primary" type="submit" value="submit">+</button> </div> </div><br><br><br> </form>修改路由支持PUT方法
如果需要保留PUT请求的语义,将路由改为支持PUT(或包含PUT的match规则):// 方式1:直接定义PUT路由 Route::put('items/{item}/updateQuantityIncrement', 'ItemController@updateQuantityIncrement') ->name('items.updateQuantityIncrement'); // 方式2:用match包含PUT和POST Route::match(['put', 'post'], 'items/{item}/updateQuantityIncrement', [ 'as' => 'items.updateQuantityIncrement', 'uses' => 'ItemController@updateQuantityIncrement' ]);
额外检查点
- 确保自定义路由在
Route::resource之前定义,避免资源路由规则覆盖自定义路由 - 执行
php artisan route:list查看实际注册的路由,确认items.updateQuantityIncrement对应的HTTP方法是否正确 - 清除路由缓存:运行
php artisan route:clear,避免旧缓存导致的路由匹配异常
内容的提问来源于stack exchange,提问作者sebi
相关产品推荐
相关产品推荐

