Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 49f589c

Browse filesBrowse files
committed
Document FQCN named controllers
1 parent f9dafd6 commit 49f589c
Copy full SHA for 49f589c

File tree

1 file changed

+27
-19
lines changed
Filter options

1 file changed

+27
-19
lines changed

‎controller/service.rst

Copy file name to clipboardExpand all lines: controller/service.rst
+27-19Lines 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
Alternatives to base Controller Methods
139149
---------------------------------------
@@ -209,15 +219,15 @@ argument:
209219
210220
# app/config/services.yml
211221
services:
212-
app.hello_controller:
222+
AppBundle\Controller\HelloController:
213223
class: AppBundle\Controller\HelloController
214224
arguments: ['@templating']
215225
216226
.. code-block:: xml
217227
218228
<!-- app/config/services.xml -->
219229
<services>
220-
<service id="app.hello_controller" class="AppBundle\Controller\HelloController">
230+
<service id="AppBundle\Controller\HelloController" class="AppBundle\Controller\HelloController">
221231
<argument type="service" id="templating"/>
222232
</service>
223233
</services>
@@ -229,10 +239,8 @@ argument:
229239
use Symfony\Component\DependencyInjection\Definition;
230240
use Symfony\Component\DependencyInjection\Reference;
231241
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'));
236244
237245
Rather than fetching the ``templating`` service from the container, you can
238246
inject *only* the exact service(s) that you need directly into the controller.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.