有一个非常简单的 html 表单(使用 Twig 模板引擎):
<form action="{{ path('simple_route') }}" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="text" name=simple-input">
<button type="submit">Отправить</button>
</form>
以及 PUT 方法的相应处理程序:
#[Route(path: '/simple-route', name: 'simple_route', methods: ['PUT'])]
public function simpleRoute(): Response
{
// ...
}
Symfony 文档说,为了发送 PUT 方法,您需要在 html 表单中添加一个隐藏字段_method。正如您在表单示例中看到的那样,我做到了。但是,我仍然收到错误:
No route found for "POST http://example.com/simple-route: Method Not Allowed (Allow: PUT)
我不知道如何在 Symfony 中将表单提交给处理程序,该处理程序侦听标准 GET/POST 以外的方法。如果有人知道,请告诉我如何摆脱这个错误。
PS 我正在使用 PHP 8.1 和 Symfony 6.0.2。
事实证明,有必要
http_method_override在框架配置文件 (config/packages/framework.yaml) 中添加一个参数:在 Symfony 6.0.4 上测试。