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

Symfony 6.4中同基础路由POST请求报405错误排查

问题原因

你的get_user路由中id的正则规则是.+,这个规则会匹配任意字符(包括斜杠/)。当请求POST /users/{id}/file时,Symfony路由匹配系统会优先命中get_user路由,把{id}/file整体当作id的参数值,但该路由仅允许GET方法,因此返回405 Method Not Allowed错误。

解决方案

修改两个路由中id的正则规则,限制id不能包含斜杠/,用[^/]+替代.+:

Get.php 控制器路由修改

#[Route('/users/{id}', name: 'get_user', methods: ['GET'], requirements: ['id' => '[^/]+'])]

AddFile.php 控制器路由修改

#[Route('/users/{id}/file', name: 'add_user_file', methods: ['POST'], requirements: ['id' => '[^/]+'])]

修改后,get_user路由仅匹配/users/xxx格式的请求(xxx不含斜杠),add_user_file路由会正确匹配/users/xxx/file的POST请求。

可再次执行php bin/console debug:router确认路由规则,之后重新测试POST请求即可正常访问。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:55:00