New in Symfony 6.1: Services in Route Conditions

Contributed by Renan de Lima in #44405.

Routing in Symfony applications is usually simple and consists of mapping some controller method to some URL path. However, sometimes you need to evaluate complex conditions to decide if some incoming URL should match a given controller. That's why Symfony allows using expressions to match routes. In Symfony 6.1 we've improved routing conditions so you can also call services inside those expressions. To do that, use the new service() function and pass the name of the service to call:

    // src/Controller/DefaultController.php

namespace App\Controller;

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

class DefaultController extends AbstractController {

[Route(

    '/some-path',
    name: 'some_name',
    condition: "service('some_service').someMethod()",
)]
public function someControllerMethod(): Response
{
    // ...
}

}

By default, and for performance reasons, you cannot call any of the services defined in your application. Instead, you must add the routing.condition_service tag or the #[AsRoutingConditionService] attribute to those services that will be available in route conditions:

    use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;

// ...

[AsRoutingConditionService(alias: 'some_service')]

class SomeService { public function someMethod(): bool { // ... } }

The alias option defines how this service will be referred to inside the expression, so you don't have to use the full service name (which is usually too long).

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-6-1-services-in-route-conditions?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | May 18, 2022, 8:20:05 AM


Login to add comment

Other posts in this group

New in Symfony 7.3: Twig Extension Attributes

Contributed by Jérôme Tamarelle in

Apr 28, 2025, 8:50:09 AM | Symfony
A Week of Symfony #956 (April 21–27, 2025)

This week, we kicked off the New in Symfony 7.3 blog series, highlighting all the exciting new features coming in this release. We also unveiled more details about some of the SymfonyOnline June 2025

Apr 27, 2025, 9:41:08 AM | Symfony
SymfonyOnline June 2025: How Doctrine Events Ruined My Day(s)

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 25, 2025, 1:40:15 PM | Symfony
Introducing A Streaming AMQP Transport for Symfony Messenger

Symfony's Messenger component makes it easy to build message-driven applications. However, developers using symfony/amqp-messenger have long faced a limitation: it relies on polling (get()), which can

Apr 25, 2025, 9:10:02 AM | Symfony
SymfonyLive Berlin 2025: Recap and Replay !

SymfonyLive Berlin 2025 took place just 3 weeks ago!

A huge thank you to everyone who joined us!🔥 The conference brought together the local Symfony community in the heart of Berlin for two da

Apr 24, 2025, 2:30:18 PM | Symfony
New in Symfony 7.3: Global Translation Parameters

Contributed by Hubert Lenoir in

Apr 24, 2025, 7:30:24 AM | Symfony
SymfonyOnline June 2025: FormFlow: Build Stunning Multistep Forms

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 23, 2025, 3:20:21 PM | Symfony