@@ -12,12 +12,14 @@ injection like any other normal service.
12
12
Referencing your Service from Routing
13
13
-------------------------------------
14
14
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.
17
18
19
+ Use the ``service_id::method_name `` syntax to refer to the controller method.
18
20
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 ``:
21
23
22
24
.. configuration-block ::
23
25
@@ -30,7 +32,7 @@ syntax in your routing and it will find your service.
30
32
// ...
31
33
32
34
/**
33
- * @Route(service="app.hello_controller ")
35
+ * @Route(service="App\Controller\HelloController::index ")
34
36
*/
35
37
class HelloController
36
38
{
@@ -42,7 +44,7 @@ syntax in your routing and it will find your service.
42
44
# config/routes.yaml
43
45
hello :
44
46
path : /hello
45
- defaults : { _controller: app.hello_controller:indexAction }
47
+ defaults : { _controller: App\Controller\HelloController::index }
46
48
47
49
.. code-block :: xml
48
50
@@ -54,7 +56,7 @@ syntax in your routing and it will find your service.
54
56
http://symfony.com/schema/routing/routing-1.0.xsd" >
55
57
56
58
<route id =" hello" path =" /hello" >
57
- <default key =" _controller" >app.hello_controller:indexAction </default >
59
+ <default key =" _controller" >App\Controller\HelloController::index </default >
58
60
</route >
59
61
60
62
</routes >
@@ -63,22 +65,17 @@ syntax in your routing and it will find your service.
63
65
64
66
// config/routes.php
65
67
$collection->add('hello', new Route('/hello', array(
66
- '_controller' => 'app.hello_controller:indexAction ',
68
+ '_controller' => 'App\Controller\HelloController::index ',
67
69
)));
68
70
69
- .. note ::
70
-
71
- When using the ``service_id:method_name `` syntax, the method name must
72
- end with the ``Action `` suffix.
73
-
74
71
.. _controller-service-invoke :
75
72
76
73
Invokable Controllers
77
74
---------------------
78
75
79
76
If your controller implements the ``__invoke() `` method - popular with the
80
77
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).
82
79
83
80
Alternatives to base Controller Methods
84
81
---------------------------------------
0 commit comments