Skip to content

Commit 953865a

Browse files
Adding full example for a Service in Route Condition
Reasons: * There needs to be an example on how to pass any information to this callback service. I merely *guessed* the `route` syntax - it's not even explained at https://symfony.com/blog/new-in-symfony-6-1-services-in-route-conditions - is there a better way to do it? * How to call a method from this service wasn't explained (the `.check()` part) * This method needs to return `bool`? I don't think it's a good idea to add those code blocks inside this indented "list" - will it even work? So probably it's better to create a dedicated paragraph (or even heading) for Services in Route Conditions. But you need to tell me how (i.e. what structure you have in mind).
1 parent 1a7913d commit 953865a

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

routing.rst

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,28 @@ You can also use these functions:
379379
``service(string $alias)``
380380
Returns a routing condition service.
381381
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.
382+
tag to your service if you want to use it in the condition. Example:
383+
384+
.. code-block:: php
385+
386+
// Controller:
387+
#[Route(condition: "service('route_checker').check(request)")]
388+
// Or without alias:
389+
#[Route(condition: "service('Ap\\\Service\\\RouteChecker').check(request)")]
390+
391+
.. code-block:: php
392+
393+
use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;
394+
use Symfony\Component\HttpFoundation\Request;
395+
396+
#[AsRoutingConditionService(alias: 'route_checker')]
397+
class RouteChecker
398+
{
399+
public function check(Request $request): bool
400+
{
401+
// ...
402+
}
403+
}
383404
384405
.. versionadded:: 6.1
385406

0 commit comments

Comments
 (0)