Using `$request->get()` in Symfony: The Current and Correct Method
For a long time, the preferred method for accessing request data in ymfony projects was:
$value = $request->get('name');
However, this usage is no longer recommended in current Symfony versions. Request::get() method was deprecated in Symfony 7.4 and is planned to be completely removed in Symfony 8.0. The main reason for this is that the $request->get() method doesn't explicitly show where the value is coming from. This method searches first the route attributes, then the GET parameters, and then the POST data. This can reduce code readability and reliability.
Symfony's recommended approach is to explicitly specify the source of the data. The HttpFoundation documentation states that $_GET is used for query , $_POST for request , and route or in-app attribute data for attributes .
Old Usage
$name = $request->get('name');
In this usage, it's unclear where name value comes from:
- A URL can be a query parameter.
- The form can be POST data.
- The route attribute can be a value.
Therefore, a more open and controlled approach should be preferred in current Symfony projects.
New and Correct Usage
For the GET parameter
For example, if the URL is like this:
/products?search=laptop&page=2
Correct usage:
$search = $request->query->get('search');
$page = $request->query->getInt('page', 1);
Here, since the data comes from the URL query string $request->query is used.
For POST Form Data
If data is being submitted from a form:
$name = $request->request->get('name');
$email = $request->request->get('email');
Here, $request->request is used because the data comes from the form's POST content.
For the Route Parameter
If the route is as follows:
#[Route('/products/{id}', name: 'product_show')]
Cleanest usage within the controller:
#[Route('/products/{id}', name: 'product_show')]
public function show(string $id): Response
{
// $id route parameter
}
Alternatively, if you need to obtain it via request:
$id = $request->attributes->get('id');
Concrete Example: Product Search Controller
In the following example, the user is searching a product list. search and page information is retrieved via the URL.
URL example:
/products?search=phone&page=2
Controller:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ProductController extends AbstractController
{
#[Route( '/products' , name: 'product_index' , methods: [ 'GET' ])]
public function index ( Request $request ): Response
{
$search = $request->query->get( 'search' , '' );
$page = $request->query->getInt( 'page' , 1 );
return $this->render( 'product/index.html.twig' , [
'search' => $search ,
'page' => $page ,
]);
}
}
It wouldn't be correct to write it here in the old style:
$search = $request->get('search');
$page = $request->get('page');
Because in this usage, Symfony might look for the value within the route, GET, or POST request. However, what we know is that search and page come from the URL. Therefore, the correct usage is:
$search = $request->query->get('search', '');
$page = $request->query->getInt('page', 1);
JSON API Example
If a JSON body is being sent to an API endpoint:
{
"name": "Test Product" ,
"price": 150
}
Within the controller, the following usage is preferable:
#[Route('/api/products', name: 'api_product_create', methods: ['POST'])]
public function create(Request $request): Response
{
$data = $request->toArray();
$name = $data['name'] ?? null;
$price = $data['price'] ?? null;
return $this->json([
'name' => $name,
'price' => $price,
]);
}
If both form data and JSON payloads are to be supported, Symfony's getPayload() method can also be used:
$payload = $request->getPayload();
$name = $payload->get('name');
$price = $payload->get('price');
Quick Comparison
| Data Source | Old Usage | New Use |
|---|---|---|
| GET parameter | $request->get('search') |
$request->query->get('search') |
| POST form data | $request->get('name') |
$request->request->get('name') |
| Route parameter | $request->get('id') |
$request->attributes->get('id') |
| JSON body | $request->get('name') |
$request->toArray() or $request->getPayload() |
Conclusion
Using $request->get() in Symfony is no longer a suitable choice for modern projects. Instead, explicit usage based on the data source should be preferred.
$request->query->get('key'); // GET
$request->request->get('key'); // POST form
$request->attributes->get('key'); // Route attribute
$request->toArray(); // JSON body
$request->getPayload(); // Form veya JSON payload
This approach makes the code more readable, safer, and more compatible with current versions of Symfony. Especially for Symfony 7.4 and later, using these methods instead of $request->get() is more appropriate in new projects.
Keywords: Symfony