@@ -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
Alternatives to base Controller Methods
139
149
---------------------------------------
@@ -209,15 +219,15 @@ argument:
209
219
210
220
# app/config/services.yml
211
221
services :
212
- app.hello_controller :
222
+ AppBundle\Controller\HelloController :
213
223
class : AppBundle\Controller\HelloController
214
224
arguments : ['@templating']
215
225
216
226
.. code-block :: xml
217
227
218
228
<!-- app/config/services.xml -->
219
229
<services >
220
- <service id =" app.hello_controller " class =" AppBundle\Controller\HelloController" >
230
+ <service id =" AppBundle\Controller\HelloController " class =" AppBundle\Controller\HelloController" >
221
231
<argument type =" service" id =" templating" />
222
232
</service >
223
233
</services >
@@ -229,10 +239,8 @@ argument:
229
239
use Symfony\Component\DependencyInjection\Definition;
230
240
use Symfony\Component\DependencyInjection\Reference;
231
241
232
- $container->setDefinition('app.hello_controller', new Definition(
233
- HelloController::class,
234
- array(new Reference('templating'))
235
- ));
242
+ $container->register(HelloController::class, HelloController::class)
243
+ ->addArgument(new Reference('templating'));
236
244
237
245
Rather than fetching the ``templating `` service from the container, you can
238
246
inject *only * the exact service(s) that you need directly into the controller.
0 commit comments