Symfony Forms 手动提交 GET 和 POST 请求

Symfony Forms manual submission of both GET and POST requests

有什么方法可以从请求 class 中获取合并数据?因为目前我们正在为接受 POST 和 GET 查询的 API 控制器手动提交表单(由于遗留项目,这不是 REST API)。

$data = array_merge($request->query->all(), $request->request->all());
$form->submit($data);

有什么方法可以写出比下面的代码更干净的东西吗?

$data = array_merge($request->query->all(), $request->request->all());

我认为不可能(也许我错了)

如果你查看 source code of Request,你可以看到当 Symfony 创建请求时,Symfony 将全局变量 $_GET 放在 $this->query 中,将 $_POST 放在 $this->request.
没有同时采用两者的 Symfony 变量。

如果你只需要一个地方,我觉得你做的很好。

如果没有,请创建一个服务或另一个考虑此代码的解决方案。

另一种解决方案是使用全局变量 $_REQUEST,因为 Symfony 使 merge,但这取决于您的 php 配置(request_order 您的参数 php.ini).
但我不认为使用 Superglobals variables with Symfony is a good idea ... (Besides symfony overwrites 他们)