How to get query string in Symfony 4

Tags: , , , ,



I am trying to access the query string parameters in Symfony 4

namespace AppController;

use SymfonyComponentHttpFoundationRequestStack;

class Home extends Controller {

    private $request;

    public function __construct(RequestStack $request){

        $this->request = $request;
    }

    public function getQueryString(){

       $req = $this->request->getCurrentRequest();

       print_r($req); // see all the request data

       // $req -> grab the query parameters
       // return query parameters
    }
}

I am using RequestStack and able to see a bunch of request data when I print the result of getCurrentRequest() (including the query parameters I need), but most of the methods are private and I am not able to access them.

How does one go about getting the request URL components (including query parameters) in Symfony?

Answer

For GET query:

$this->request->getCurrentRequest()->query->get('name_query');

For POST query:

$this->request->getCurrentRequest()->request->get('name_query');


Source: stackoverflow