You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 11中PUT/PATCH/DELETE请求返回405错误求助

Laravel 11 API PUT/PATCH/DELETE请求返回405 Method Not Allowed错误

错误详情

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
PHP 8.2.24
Laravel 11.19.0
The PUT method is not supported for route /. Supported methods: GET, HEAD.

开发Laravel 11项目API时,PUT、PATCH、DELETE请求持续返回405错误,但GET和POST请求完全正常。已确认服务器能正确接收PUT/PATCH/DELETE请求,Laravel也可识别请求方法,但仍触发上述异常。

已注册路由

通过php artisan route:list验证路由已正确注册,相关路由信息如下:

GET|HEAD        api/v2/categories ........................... categories.index › Api\CategoryController@index
POST            api/v2/categories ........................... categories.store › Api\CategoryController@store
GET|HEAD        api/v2/categories/{category} .................. categories.show › Api\CategoryController@show
PUT|PATCH       api/v2/categories/{category} .............. categories.update › Api\CategoryController@update
DELETE          api/v2/categories/{category} ............ categories.destroy › Api\CategoryController@destroy
GET|HEAD        dashboard/categories ............... dashboard.categories › App\Livewire\Dashboard\Categories
GET|HEAD        dashboard/categories/{category}/edit .. categories.edit › App\Livewire\Dashboard\EditCategory

API路由定义代码:

Route::middleware([ApiTokenMiddleware::class])
    ->prefix('v2')
    ->group(function () {
        Route::put('/categories/{category}', [CategoryController::class, 'update']);
    });

已尝试的排查步骤

  • 服务器配置:确认Apache/Nginx支持PUT和DELETE方法;
  • Laravel路由:通过php artisan route:list验证路由注册无误;
  • 请求验证:确认请求确实以PUT/PATCH/DELETE方法到达服务器。

更新1:进一步测试结果

排查Nginx、Apache配置及.htaccess文件后,确认服务器端无问题。反复测试发现PUT请求可正常通过,但存在不明重定向,问题根源在代码层面。请求能通过自定义中间件,但后续触发异常。

编写测试路由(禁用CSRF保护)可正常响应,无重定向问题:

Route::put('api/v2/categories/{id}', function($id) {
    echo 'He-He!' . $id;
});

内容的提问来源于stack exchange,提问作者neXau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 04:57:03