4

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

namespace App\Controller;

use Symfony\Component\HttpFoundation\RequestStack;

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?

0

3 Answers 3

7

For GET query:

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

For POST query:

$this->request->getCurrentRequest()->request->get('name_query');
Sign up to request clarification or add additional context in comments.

Comments

3
// retrieves $_GET and $_POST variables respectively
$request->query->get('id');
$request->request->get('category', 'default category');

Source https://symfony.com/doc/4.3/introduction/http_fundamentals.html#symfony-request-object

Comments

2

This works with Symfony 6:

GET

$value = $request->query->get('name_query');

POST

$value = $request->request->get('name_query');

BODY (REST API returns JSON response inside body)

$value = $request->getContent();

HEADER

$value = $request->headers->get('X-header-name');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.