Skip to content

[FrameworkBundle] fix docs for controller as services #15756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ How to Define Controllers as Services

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

Referencing your Service from Routing
-------------------------------------
If your controllers don't extend the `AbstractController`_ class, you must explicitly mark your controller
services as ``public``.
Alternatively, you can also tag controller services with the ``controller.service_arguments``.
This will make the tagged services ``public`` and will allow you to inject services in method parameters:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml

# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']

Registering your controller as a service is the first step, but you also need to
update your routing config to reference the service properly, so that Symfony
Expand Down Expand Up @@ -222,5 +237,4 @@ If you want to know what type-hints to use for each service, see the

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