Skip to content

Commit f226cdd

Browse files
committed
minor #17212 [Routing] Adding full example for a Service in Route Condition (ThomasLandauer)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [Routing] 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). Commits ------- 4e72ef3 [Routing] Adding full example for a Service in Route Condition
2 parents 9d0d337 + 4e72ef3 commit f226cdd

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

routing.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,27 @@ 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::
383+
384+
385+
// Controller (using an alias):
386+
#[Route(condition: "service('route_checker').check(request)")]
387+
// Or without alias:
388+
#[Route(condition: "service('Ap\\\Service\\\RouteChecker').check(request)")]
389+
390+
.. code-block:: php
391+
392+
use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;
393+
use Symfony\Component\HttpFoundation\Request;
394+
395+
#[AsRoutingConditionService(alias: 'route_checker')]
396+
class RouteChecker
397+
{
398+
public function check(Request $request): bool
399+
{
400+
// ...
401+
}
402+
}
383403
384404
.. versionadded:: 6.1
385405

0 commit comments

Comments
 (0)