Getting all request parameters in Symfony 2

后端 未结 3 1747
梦毁少年i
梦毁少年i 2020-12-13 08:05

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

$this->getRequest()->get(\'value1\');
$this->getRequest()->ge         


        
相关标签:
3条回答
  • 2020-12-13 08:42

    Since you are in a controller, We assume to you are passing parameter Request; You can access all post $request->request->all(); This returns key-value pair array; On the other when using get request you access data using $request->query->all();

    0 讨论(0)
  • 2020-12-13 08:47

    With Recent Symfony 2.6+ versions as a best practice Request is passed as an argument with action in that case you won't 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());
            }
    
        }
    
    0 讨论(0)
  • You can do $this->getRequest()->query->all(); to get all GET params and $this->getRequest()->request->all(); to get all POST params.

    So in your case:

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

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

    0 讨论(0)
提交回复
热议问题