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