Retrieving all request parameters in Symfony 2

In symfony 2 controllers, every time I want to get a value from a message, I need to run:

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

Is there a way to combine them into a single statement that returns an array? Something like Zend getParams ()?

+53
php symfony request
Jun 27 2018-12-12T00:
source share
2 answers

You can do $this->getRequest()->query->all(); to get all GET and $this->getRequest()->request->all(); parameters $this->getRequest()->request->all(); to get all the POST options.

So in your case:

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

For more information about the Request class, see http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html

+118
Jun 27. '12 at 13:30
source share
β€” -

In recent versions of Symfony 2.6+, as a best practice, the request is passed as an argument with an action in which case you do not need to explicitly call $ this-> getRequest (), but rather call $ 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()); } } 
+4
Oct. 25 '15 at 9:16
source share



All Articles