Skip to content

Commit d73643a

Browse files
committed
Updated an example about controllers as services
1 parent 8e64505 commit d73643a

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

controller/service.rst

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ injection like any other normal service.
1212
Referencing your Service from Routing
1313
-------------------------------------
1414

15-
Registering your controller as a service is great, but you also need to make sure
16-
that your routing references the service properly, so that Symfony knows to use it.
15+
Registering your controller as a service is the first step, but you also need to
16+
update your routing config to reference the service properly, so that Symfony
17+
knows to use it.
1718

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

2224
.. configuration-block::
2325

@@ -30,7 +32,7 @@ syntax in your routing and it will find your service.
3032
// ...
3133
3234
/**
33-
* @Route(service="app.hello_controller")
35+
* @Route(service="App\Controller\HelloController::index")
3436
*/
3537
class HelloController
3638
{
@@ -42,7 +44,7 @@ syntax in your routing and it will find your service.
4244
# config/routes.yaml
4345
hello:
4446
path: /hello
45-
defaults: { _controller: app.hello_controller:indexAction }
47+
defaults: { _controller: App\Controller\HelloController::index }
4648
4749
.. code-block:: xml
4850
@@ -54,7 +56,7 @@ syntax in your routing and it will find your service.
5456
http://symfony.com/schema/routing/routing-1.0.xsd">
5557
5658
<route id="hello" path="/hello">
57-
<default key="_controller">app.hello_controller:indexAction</default>
59+
<default key="_controller">App\Controller\HelloController::index</default>
5860
</route>
5961
6062
</routes>
@@ -63,22 +65,17 @@ syntax in your routing and it will find your service.
6365
6466
// config/routes.php
6567
$collection->add('hello', new Route('/hello', array(
66-
'_controller' => 'app.hello_controller:indexAction',
68+
'_controller' => 'App\Controller\HelloController::index',
6769
)));
6870
69-
.. note::
70-
71-
When using the ``service_id:method_name`` syntax, the method name must
72-
end with the ``Action`` suffix.
73-
7471
.. _controller-service-invoke:
7572

7673
Invokable Controllers
7774
---------------------
7875

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

8380
Alternatives to base Controller Methods
8481
---------------------------------------

0 commit comments

Comments
 (0)