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

Erstellt 2y | 18.05.2022, 08:20:05


Melden Sie sich an, um einen Kommentar hinzuzufügen

Andere Beiträge in dieser Gruppe

SymfonyOnline June 2024: How to Test an External API with 0 Mocks?

SymfonyOnline June 2024 is in less than 2 months: on June 6-7, get ready for the impressive lineup of speakers and topics. Visit here to learn more and don't miss out on this exciting opportuni

19.04.2024, 14:40:02 | Symfony
SymfonyLive Berlin 2024 This is a test: One-click Cypress.IO E2E testing in 45 seconds

SymfonyLive Berlin 2024 start in 2 months: on June 20-21, get ready for the impressive lineup of speakers and topics. Visit here to learn more and don't miss out on this exciting opportunity!

19.04.2024, 10:10:10 | Symfony
SymfonyOnline June 2024 - Carry out the best audit for your client

SymfonyOnline June 2024 is in less than 2 months: on June 6-7, get ready for the impressive lineup of speakers and topics. Visit here to learn more and don't miss out on this exciting opportuni

18.04.2024, 15:50:12 | Symfony
SymfonyLive Berlin 2024 Plants vs thieves: Web Security and Tests!?

SymfonyLive Berlin 2024 start in 2 months: on June 20-21, get ready for the impressive lineup of speakers and topics. Visit here to learn more and don't miss out on this exciting opportunity!

18.04.2024, 15:50:11 | Symfony
SymfonyOnline June 2024 - Practical insight into modulithic architecture

SymfonyOnline June 2024 is in less than 2 months: on June 6-7, get ready for the impressive lineup of speakers and topics. Visit here to learn more and don't miss out on this exciting opportuni

17.04.2024, 14:50:02 | Symfony
A Week of Symfony #902 (8-14 April 2024)

This was a very special week for the Symfony project. Ryan Weaver, a beloved Symfony core team member and SymfonyCasts founder, asked for our love and support as he battles cancer. The Symfony and PHP

14.04.2024, 07:10:15 | Symfony
Ryan Weaver Needs Our Help

Ryan Weaver, a cherished member of the Symfony community, is in urgent need of our help. He's battling cancer and he's asking for our love and support. Please, if you're able to, make a donation today

09.04.2024, 15:20:29 | Symfony