Skip to content

Updated an example about controllers as services #9470

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
Mar 23, 2018
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
25 changes: 11 additions & 14 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ injection like any other normal service.
Referencing your Service from Routing
-------------------------------------

Registering your controller as a service is great, but you also need to make sure
that your routing references the service properly, so that Symfony knows to use it.
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
knows to use it.

Use the ``service_id::method_name`` syntax to refer to the controller method.
If the service id is the fully-qualified class name (FQCN) of your controller,
you're done! You can use the normal ``App\Controller\HelloController::index``
syntax in your routing and it will find your service.
as Symfony recommends, then you can use something like:
``App\Controller\HelloController::index``:

.. configuration-block::

Expand All @@ -30,7 +32,7 @@ syntax in your routing and it will find your service.
// ...

/**
* @Route(service="app.hello_controller")
* @Route(service="App\Controller\HelloController::index")
*/
class HelloController
{
Expand All @@ -42,7 +44,7 @@ syntax in your routing and it will find your service.
# config/routes.yaml
hello:
path: /hello
defaults: { _controller: app.hello_controller:indexAction }
defaults: { _controller: App\Controller\HelloController::index }

.. code-block:: xml

Expand All @@ -54,7 +56,7 @@ syntax in your routing and it will find your service.
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="hello" path="/hello">
<default key="_controller">app.hello_controller:indexAction</default>
<default key="_controller">App\Controller\HelloController::index</default>
</route>

</routes>
Expand All @@ -63,22 +65,17 @@ syntax in your routing and it will find your service.

// config/routes.php
$collection->add('hello', new Route('/hello', array(
'_controller' => 'app.hello_controller:indexAction',
'_controller' => 'App\Controller\HelloController::index',
)));

.. note::

When using the ``service_id:method_name`` syntax, the method name must
end with the ``Action`` suffix.

.. _controller-service-invoke:

Invokable Controllers
---------------------

If your controller implements the ``__invoke()`` method - popular with the
Action-Domain-Response (ADR) pattern, you can simply refer to the service id
(``App\Controller\HelloController`` or ``app.hello_controller`` for example).
(``App\Controller\HelloController`` for example).

Alternatives to base Controller Methods
---------------------------------------
Expand Down