Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

New in Symfony 5.1: Routing improvements

Symfony 5.1 adds important new features related to routing, such as priority for route annotations and simpler route config. In this article we'll show other minor but interesting features added to routing.

Added stateless route attribute

Contributed by
Mathias Arlaud
in #35732 and #35782.

Routes can now configure a stateless boolean option. If set to true, they declare that session won't be used during the handling of the request.

If a stateless route uses the session, you'll see an exception when debug is enabled in the application and you'll get a log message when debug is disabled:

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

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

class MainController extends AbstractController
{
    /**
     * @Route("/", name="homepage", stateless=true)
     */
    public function homepage()
    {
        // ...
    }
}

Allow using env vars in route conditions

Contributed by
Ahmed Tailouloute
in #35747.

Routing conditions define expressions that routes must match. In Symfony 5.1, we've improved those expressions to allow using environment variables.

When using env vars, you can also apply any of the Symfony env var processors:

1
2
3
4
5
6
7
8
/**
 * @Route("/new-feature", condition="env('bool:IS_FEATURE_ENABLED') === true")
 */
public function __invoke()
{
    // this route will only execute when the value of the
    // IS_FEATURE_ENABLED env var is TRUE
}

Simpler RequestContext configuration

Contributed by
Benjamin Lévêque
in #35281.

Caution

This section explained a feature that was merged in Symfony 5.1 but then it was replaced by this other feature before the final release of Symfony 5.1.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

These are great improvements! Especially the stateless route attribute. We would have needed this multiple times in the past.

Big +1 :-)
What is the purpose of stateless=true? Performance improvement because the session does not have to be started? Easier caching in background?

What if a stateless controller renders a twig template which renders a stateful controller?
Are these inheritable, so you can put them on all actions in a given controller, or all controllers in a folder?
@Dan Blows

Yes you can :)

@Daniel Sentker

It will raise an exception, since the route is stateless a sub request should be too.
Great job!
Sorry guys, but I'm not an expert Symfony developer (as you will see in this meantime!), I can't understand stateless utility. Can you give me an example, please?
thanks!!!!!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.