symfony The Request Getting a query string parameter


Example

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:

http://example.com/products?page=3

The raw HTTP request would look something like this:

GET /products?page=3 HTTP/1.1
Host: example.com
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 http://example.com/products (note the absence of the query string), the $page variable will contain the default value 1.