New in Symfony 5.4: Controller Changes

Symfony Controllers are the "glue code" that runs some logic and calls some services to serve each application route. They are a very stable piece of software that we rarely change. However, in Symfony 5.4 we've made some changes to controllers that may impact your applications.

Deprecated the Request::get() Method

        Contributed by Roland Franssen 
        in #42392.

The Symfony Request Object is an object-oriented representation of the HTTP request message. This object provides several methods to get information from the incoming request:

    1

2 3 4 5 6 7 8 // retrieve information from $_GET $request->query->get('id'); // retrieve information from $_POST $request->request->get('category', 'default category'); // retrieve information from $_SERVER $request->server->get('HTTP_HOST'); // retrieve information from $_COOKIE $request->cookies->get('PHPSESSID');

In addition to these specific methods, there's a generic get() method that looks for information in path (routing placeholders or custom attributes), $_GET, and $_POST and returns the first value found:

    1

2 // this information could come from route attributes, from $_GET or form $_POST $request->get('id');

The flexibility of this method could be useful in some edge-cases, but it's generally better to be explicit about where does data come from. That's why we've been discouraging the usage of this method from some years and in Symfony 5.4 we're marking it as private. You can still use it, but you'll see deprecation messages if you do, so it's better if you start upgrading your applications.

Deprecated Some Controller Shortcuts

        Contributed by Fabien Potencier 
        in #42422
        and #42442.

In early Symfony versions, you could access all your application services from the controllers using the get() and has() methods:

    1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // src/Controller/SomeController.php namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route;

class SomeController extends AbstractController {

[Route(...)]

public function someAction(): Response
{
    $doctrine = $this->get('doctrine');
    // ...
}

}

Later we removed this feature because accessing the entire service container in this way is considered an anti-pattern. Therefore, the get() method only allows access to a very limited set of services related to controllers. In Symfony 5.4 we're deprecating the get() and has() methods entirely. Instead, fetching services in controllers should use constructor or method injection. Besides, controllers provide a series of shortcuts for the most common operations. For example, to redirect to some route, you don't need to inject the UrlGeneratorInterface class to get the URL generator service. You can optionally use the redirectToRoute() shortcut:

    1

2 3 4 5 6 7

[Route(...)]

public function someAction(): Response { // ...

return $this->redirectToRoute('...');

}

In addition to this change, we reviewed the list of shortcuts to see if we should add or remove some. We decided to deprecate the following controller shortcuts because they are not directly related to HTTP operations: dispatchMessage() getDoctrine() Instead of using those shortcuts, inject the related services in the constructor or the controller methods.

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-5-4-controller-changes?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | Nov 11, 2021, 2:20:08 AM


Login to add comment

Other posts in this group

A Week of Symfony #943 (20-26 January 2025)

This week, the upcoming Symfony 7.3 version improved the invokable command feature, deprecated the use of option arrays for configuring validation constraints, and updated the JsonEncoder component to

Jan 26, 2025, 12:10:08 PM | Symfony
SymfonyLive Berlin 2025: Need a MACH-ready Search Engine?

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

As we are now unveiling th

Jan 24, 2025, 11:20:16 AM | Symfony
SymfonyLive Paris 2025 : Rôles & permissions : développez une marque blanche avec du Feature Flipping

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Jan 23, 2025, 4:50:03 PM | Symfony
SymfonyLive Berlin 2025: So you think you know PHPUnit

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

First, a big thank you to

Jan 22, 2025, 8:20:10 AM | Symfony
SymfonyLive Paris 2025 : Passkeys pour une authentification fluide et sécurisée

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

To

Jan 21, 2025, 11:30:10 AM | Symfony
Join us for SymfonyDay Chicago – March 17, 2025!

Mark your calendars for March 17, 2025 because SymfonyDay Chicago 2025 promises to be a one-of-a-kind event that you won’t want to miss! This full day is dedicated to celebrating the incredible contri

Jan 20, 2025, 7:20:03 PM | Symfony
A Week of Symfony #942 (13-19 January 2025)

This week, Symfony celebrated the SymfonyOnline January 2025 conference. In addition, it announced the new Symfony UX Core Team. Lastly, the upcoming Symfony 7.3 version simplified the configuration o

Jan 19, 2025, 8:30:08 AM | Symfony