Skip to content

Commit 4ccf907

Browse files
committed
minor #15756 [FrameworkBundle] fix docs for controller as services (dunglas)
This PR was squashed before being merged into the 5.3 branch. Discussion ---------- [FrameworkBundle] fix docs for controller as services Without this change, controllers not extending `AbstractController` aren't registered as services since symfony/recipes#915. Commits ------- cad2c97 [FrameworkBundle] fix docs for controller as services
2 parents 0f85c22 + cad2c97 commit 4ccf907

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

controller/service.rst

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@ How to Define Controllers as Services
66

77
In Symfony, a controller does *not* need to be registered as a service. But if you're
88
using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
9-
your controllers *are* already registered as services. This means you can use dependency
9+
and that your controllers extend the `AbstractController`_ class,
10+
they *are* already registered as services. This means you can use dependency
1011
injection like any other normal service.
1112

12-
Referencing your Service from Routing
13-
-------------------------------------
13+
If your controllers don't extend the `AbstractController`_ class, you must explicitly mark your controller
14+
services as ``public``.
15+
Alternatively, you can also tag controller services with the ``controller.service_arguments``.
16+
This will make the tagged services ``public`` and will allow you to inject services in method parameters:
17+
18+
.. configuration-block::
19+
20+
.. code-block:: yaml
21+
22+
# config/services.yaml
23+
24+
# controllers are imported separately to make sure services can be injected
25+
# as action arguments even if you don't extend any base controller class
26+
App\Controller\:
27+
resource: '../src/Controller/'
28+
tags: ['controller.service_arguments']
1429
1530
Registering your controller as a service is the first step, but you also need to
1631
update your routing config to reference the service properly, so that Symfony
@@ -222,5 +237,4 @@ If you want to know what type-hints to use for each service, see the
222237

223238
.. _`Controller class source code`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
224239
.. _`AbstractController`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
225-
.. _`AbstractController`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
226240
.. _`ADR pattern`: https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder

0 commit comments

Comments
 (0)