New in Symfony 5.1: Routing improvements
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Big +1 :-)
What if a stateless controller renders a twig template which renders a stateful controller?
Yes you can :)
@Daniel Sentker
It will raise an exception, since the route is stateless a sub request should be too.
thanks!!!!!