Skip to content

Commit 6e8ddeb

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: [Routing] Adding full example for a Service in Route Condition
2 parents 5bd86fb + 8c3cb80 commit 6e8ddeb

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

routing.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,28 @@ You can also use these functions:
378378

379379
``service(string $alias)``
380380
Returns a routing condition service.
381-
You'll have to add the ``#[AsRoutingConditionService]`` attribute or ``routing.condition_service``
382-
tag to your service if you want to use it in the condition.
381+
382+
First, add the ``#[AsRoutingConditionService]`` attribute or ``routing.condition_service``
383+
tag to the services that you want to use in route conditions::
384+
385+
use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;
386+
use Symfony\Component\HttpFoundation\Request;
387+
388+
#[AsRoutingConditionService(alias: 'route_checker')]
389+
class RouteChecker
390+
{
391+
public function check(Request $request): bool
392+
{
393+
// ...
394+
}
395+
}
396+
397+
Then, use the ``service()`` function to refer to that service inside conditions::
398+
399+
// Controller (using an alias):
400+
#[Route(condition: "service('route_checker').check(request)")]
401+
// Or without alias:
402+
#[Route(condition: "service('Ap\\\Service\\\RouteChecker').check(request)")]
383403

384404
.. versionadded:: 6.1
385405

0 commit comments

Comments
 (0)