获取Symfony 2中的所有请求参数

在symfony 2控制器中,每次我想从后期得到一个值,我需要运行:

$this->getRequest()->get('value1'); $this->getRequest()->get('value2'); 

有什么办法可以将这些语句合并成一个返回数组的语句吗? 像Zend的getParams()?

你可以做$this->getRequest()->query->all(); 得到所有的GET参数和$this->getRequest()->request->all(); 获取所有POST参数。

所以在你的情况下:

 $params = $this->getRequest()->request->all(); $params['value1']; $params['value2']; 

有关Request类的更多信息,请参阅http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html

使用最近的Symfony 2.6+版本作为最佳实践请求被作为parameter passing,在这种情况下,您不需要显式调用$ this-> getRequest(),而是调用$ request-> request-> all()

 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; use Symfony\Component\HttpFoundation\RedirectResponse; class SampleController extends Controller { public function indexAction(Request $request) { var_dump($request->request->all()); } }