Skip to content

Commit 99ba301

Browse files
committed
Document FQCN named controllers
1 parent 68deeb8 commit 99ba301

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

controller/service.rst

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,39 @@ Then you can define it as a service as follows:
6666
6767
# app/config/services.yml
6868
services:
69-
app.hello_controller:
69+
AppBundle\Controller\HelloController:
7070
class: AppBundle\Controller\HelloController
7171
7272
.. code-block:: xml
7373
7474
<!-- app/config/services.xml -->
7575
<services>
76-
<service id="app.hello_controller" class="AppBundle\Controller\HelloController" />
76+
<service id="AppBundle\Controller\HelloController" class="AppBundle\Controller\HelloController" />
7777
</services>
7878
7979
.. code-block:: php
8080
8181
// app/config/services.php
8282
use AppBundle\Controller\HelloController;
8383
84-
$container->register('app.hello_controller', HelloController::class);
84+
$container->register(HelloController::class, HelloController::class);
8585
8686
Referring to the Service
8787
------------------------
8888

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``::
9293

93-
$this->forward('app.hello_controller:indexAction', array('name' => $name));
94+
$this->forward('AppBundle:Hello:index', array('name' => $name));
9495

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``::
96100

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));
99102

100103
You can also route to the service by using the same notation when defining
101104
the route ``_controller`` value:
@@ -123,17 +126,24 @@ the route ``_controller`` value:
123126
'_controller' => 'app.hello_controller:indexAction',
124127
)));
125128
129+
.. note::
130+
131+
You cannot drop the ``Action`` part of the method name when using this
132+
syntax.
133+
126134
.. tip::
127135

128136
You can also use annotations to configure routing using a controller
129137
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.
132141

133142
.. tip::
134143

135144
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).
137147

138148
.. versionadded:: 2.6
139149
Support for ``__invoke()`` was introduced in Symfony 2.6.
@@ -212,15 +222,15 @@ argument:
212222
213223
# app/config/services.yml
214224
services:
215-
app.hello_controller:
225+
AppBundle\Controller\HelloController:
216226
class: AppBundle\Controller\HelloController
217227
arguments: ['@templating']
218228
219229
.. code-block:: xml
220230
221231
<!-- app/config/services.xml -->
222232
<services>
223-
<service id="app.hello_controller" class="AppBundle\Controller\HelloController">
233+
<service id="AppBundle\Controller\HelloController" class="AppBundle\Controller\HelloController">
224234
<argument type="service" id="templating"/>
225235
</service>
226236
</services>
@@ -232,10 +242,8 @@ argument:
232242
use Symfony\Component\DependencyInjection\Definition;
233243
use Symfony\Component\DependencyInjection\Reference;
234244
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'));
239247
240248
Rather than fetching the ``templating`` service from the container, you can
241249
inject *only* the exact service(s) that you need directly into the controller.

0 commit comments

Comments
 (0)