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