@@ -14,13 +14,26 @@ your controllers as services in several ways:
1414#. Using the ``#[AsController] `` attribute;
1515#. Using the ``controller.service_arguments `` service tag.
1616
17+ All three approaches rely on the same underlying mechanism: they apply the
18+ ``controller.service_arguments `` tag to the controller service. Symfony then
19+ marks any service carrying that tag as public and non-lazy, and enables
20+ :ref: `service argument injection <controller-accessing-services >` on its
21+ action methods. The three options only differ in how you opt in to that tag.
22+
23+ The services are public because Symfony's controller resolver fetches them
24+ from the container by their service id at runtime (for example, when a route
25+ points to ``App\Controller\HelloController::index ``), and private services
26+ cannot be retrieved that way. They are also marked non-lazy because the resolver
27+ invokes the controller immediately after fetching it, so proxy indirection
28+ would be unnecessary.
29+
1730Using the ``#[Route] `` Attribute
1831--------------------------------
1932
2033When using :ref: `the #[Route] attribute <routing-route-attributes >` to define
21- routes on any PHP class, Symfony treats that class as a controller. It registers
22- it as a public, non-lazy service and enables service argument injection in all
23- its methods .
34+ routes on any PHP class, Symfony automatically applies the
35+ `` controller.service_arguments `` tag to it. As explained above, this registers
36+ the class as a public, non-lazy service and enables service argument injection .
2437
2538This is the simplest and recommended way to register controllers as services
2639when not extending the base controller class.
@@ -29,7 +42,9 @@ Using the ``#[AsController]`` Attribute
2942---------------------------------------
3043
3144If you prefer, you can use the ``#[AsController] `` PHP attribute to automatically
32- apply the ``controller.service_arguments `` tag to your controller services::
45+ apply the ``controller.service_arguments `` tag to your controller services.
46+ The end result is the same as with ``#[Route] ``: the class becomes a public,
47+ non-lazy service with service argument injection enabled on its action methods::
3348
3449 // src/Controller/HelloController.php
3550 namespace App\Controller;
@@ -163,9 +178,12 @@ a service like: ``App\Controller\HelloController::index``:
163178Invokable Controllers
164179---------------------
165180
166- Controllers can also define a single action using the ``__invoke() `` method,
167- which is a common practice when following the `ADR pattern `_
168- (Action-Domain-Responder):
181+ Any controller (whether or not it is registered as a service) can also define a
182+ single action using the ``__invoke() `` method, which is a common practice when
183+ following the `ADR pattern `_ (Action-Domain-Responder). When the class is
184+ registered as a service (for example via ``#[Route] `` on the class, as shown
185+ below), the ``__invoke() `` method benefits from service argument injection just
186+ like any other action method:
169187
170188.. configuration-block ::
171189
0 commit comments