在 Symfony 2 中获取所有请求参数 [英] Getting all request parameters in Symfony 2
问题描述
在 symfony 2 控制器中,每次我想从帖子中获取值时,我都需要运行:
In symfony 2 controllers, every time I want to get a value from post I need to run:
$this->getRequest()->get('value1');
$this->getRequest()->get('value2');
有什么方法可以将这些合并为一个返回数组的语句?像 Zend 的 getParams() 之类的东西?
Is there any way to consolidate these into one statement that would return an array? Something like Zend's getParams()?
推荐答案
你可以通过 $this->getRequest()->query->all();
来获取所有的 GETparams 和 $this->getRequest()->request->all();
以获取所有 POST 参数.
You can do $this->getRequest()->query->all();
to get all GET params and $this->getRequest()->request->all();
to get all POST params.
所以在你的情况下:
$params = $this->getRequest()->request->all();
$params['value1'];
$params['value2'];
有关 Request 类的更多信息,请参阅 http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html
For more info about the Request class, see http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html
这篇关于在 Symfony 2 中获取所有请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!