@@ -66,36 +66,39 @@ Then you can define it as a service as follows:
66
66
67
67
# app/config/services.yml
68
68
services :
69
- app.hello_controller :
69
+ AppBundle\Controller\HelloController :
70
70
class : AppBundle\Controller\HelloController
71
71
72
72
.. code-block :: xml
73
73
74
74
<!-- app/config/services.xml -->
75
75
<services >
76
- <service id =" app.hello_controller " class =" AppBundle\Controller\HelloController" />
76
+ <service id =" AppBundle\Controller\HelloController " class =" AppBundle\Controller\HelloController" />
77
77
</services >
78
78
79
79
.. code-block :: php
80
80
81
81
// app/config/services.php
82
82
use AppBundle\Controller\HelloController;
83
83
84
- $container->register('app.hello_controller' , HelloController::class);
84
+ $container->register(HelloController::class , HelloController::class);
85
85
86
86
Referring to the Service
87
87
------------------------
88
88
89
- To refer to a controller that's defined as a service, use the single colon (:)
90
- notation. For example, to forward to the ``indexAction() `` method of the service
91
- defined above with the id ``app.hello_controller ``::
89
+ If the fully-qualified class name (FQCN) of your controller is also the id of
90
+ your service then you can refer to your controller using the usual notations.
91
+ For example, to forward to the ``indexAction() `` method of the service
92
+ defined above with the id ``AppBundle\Controller\HelloController ``::
92
93
93
- $this->forward('app.hello_controller:indexAction ', array('name' => $name));
94
+ $this->forward('AppBundle:Hello:index ', array('name' => $name));
94
95
95
- .. note ::
96
+ To refer to a controller that's defined as a service whose ID is not your
97
+ controller fully-qualified class name (FQCN), use the single colon (:)
98
+ notation. For example, to forward to the ``indexAction() `` method of a service
99
+ defined with the id ``app.hello_controller ``::
96
100
97
- You cannot drop the ``Action `` part of the method name when using this
98
- syntax.
101
+ $this->forward('app.hello_controller:indexAction', array('name' => $name));
99
102
100
103
You can also route to the service by using the same notation when defining
101
104
the route ``_controller `` value:
@@ -123,17 +126,24 @@ the route ``_controller`` value:
123
126
'_controller' => 'app.hello_controller:indexAction',
124
127
)));
125
128
129
+ .. note ::
130
+
131
+ You cannot drop the ``Action `` part of the method name when using this
132
+ syntax.
133
+
126
134
.. tip ::
127
135
128
136
You can also use annotations to configure routing using a controller
129
137
defined as a service. Make sure you specify the service ID in the
130
- ``@Route `` annotation. See the `FrameworkExtraBundle documentation `_ for
131
- details.
138
+ ``@Route `` annotation if your service ID is not your controller
139
+ fully-qualified class name (FQCN). See the
140
+ `FrameworkExtraBundle documentation `_ for details.
132
141
133
142
.. tip ::
134
143
135
144
If your controller implements the ``__invoke() `` method, you can simply
136
- refer to the service id (``app.hello_controller ``).
145
+ refer to the service id (``AppBundle\Controller\HelloController `` or
146
+ ``app.hello_controller `` for example).
137
147
138
148
.. versionadded :: 2.6
139
149
Support for ``__invoke() `` was introduced in Symfony 2.6.
@@ -212,15 +222,15 @@ argument:
212
222
213
223
# app/config/services.yml
214
224
services :
215
- app.hello_controller :
225
+ AppBundle\Controller\HelloController :
216
226
class : AppBundle\Controller\HelloController
217
227
arguments : ['@templating']
218
228
219
229
.. code-block :: xml
220
230
221
231
<!-- app/config/services.xml -->
222
232
<services >
223
- <service id =" app.hello_controller " class =" AppBundle\Controller\HelloController" >
233
+ <service id =" AppBundle\Controller\HelloController " class =" AppBundle\Controller\HelloController" >
224
234
<argument type =" service" id =" templating" />
225
235
</service >
226
236
</services >
@@ -232,10 +242,8 @@ argument:
232
242
use Symfony\Component\DependencyInjection\Definition;
233
243
use Symfony\Component\DependencyInjection\Reference;
234
244
235
- $container->setDefinition('app.hello_controller', new Definition(
236
- HelloController::class,
237
- array(new Reference('templating'))
238
- ));
245
+ $container->register(HelloController::class, HelloController::class)
246
+ ->addArgument(new Reference('templating'));
239
247
240
248
Rather than fetching the ``templating `` service from the container, you can
241
249
inject *only * the exact service(s) that you need directly into the controller.
0 commit comments