symfony The Request Getting a query string parameter


Lets say we want to build a paginated list of products, where the number of the page is passed as a query string parameter. For instance, to fetch the 3rd page, you'd go to:

The raw HTTP request would look something like this:

GET /products?page=3 HTTP/1.1
Accept: text/html
User-Agent: Mozilla/5.0 (Macintosh)

In order to get the page number from the request object, you can access the query property:

$page = $request->query->get('page'); // 3

In the case of a page parameter, you will probably want to pass a default value in case the query string parameter is not set:

$page = $request->query->get('page', 1);

This means that when someone visits (note the absence of the query string), the $page variable will contain the default value 1.