8000 feature #7864 [3.3] Document FQCN named controllers (GuilhemN, javier… · symfony/symfony-docs@a9f975b · GitHub
[go: up one dir, main page]

Skip to content

Commit a9f975b

Browse files
committed
feature #7864 [3.3] Document FQCN named controllers (GuilhemN, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- [3.3] Document FQCN named controllers Adaptation of #7771 for the master branch. Commits ------- 5fb2003 Minor reword 588333a [3.3] Document FQCN named controllers 49f589c Document FQCN named controllers
2 parents dc9ac9b + 5fb2003 commit a9f975b

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

controller/service.rst

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,36 @@ Then you can define it as a service as follows:
6666
6767
# app/config/services.yml
6868
services:
69-
app.hello_controller:
70-
class: AppBundle\Controller\HelloController
69+
AppBundle\Controller\HelloController: ~
7170
7271
.. code-block:: xml
7372
7473
<!-- app/config/services.xml -->
7574
<services>
76-
<service id="app.hello_controller" class="AppBundle\Controller\HelloController" />
75+
<service id="AppBundle\Controller\HelloController" />
7776
</services>
7877
7978
.. code-block:: php
8079
8180
// app/config/services.php
8281
use AppBundle\Controller\HelloController;
8382
84-
$container->register('app.hello_controller', HelloController::class);
83+
$container->register(HelloController::class);
8584
8685
Referring to the Service
8786
------------------------
8887

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::
9292

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

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

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

100100
You can also route to the service by using the same notation when defining
101101
the route ``_controller`` value:
@@ -123,17 +123,24 @@ the route ``_controller`` value:
123123
'_controller' => 'app.hello_controller:indexAction',
124124
)));
125125
126+
.. note::
127+
128+
You cannot drop the ``Action`` part of the method name when using the
129+
single colon notation.
130+
126131
.. tip::
127132

128133
You can also use annotations to configure routing using a controller
129134
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.
132138

133139
.. tip::
134140

135141
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).
137144

138145
Alternatives to base Controller Methods
139146
---------------------------------------
@@ -209,15 +216,14 @@ argument:
209216
210217
# app/config/services.yml
211218
services:
212-
app.hello_controller:
213-
class: AppBundle\Controller\HelloController
219+
AppBundle\Controller\HelloController:
214220
arguments: ['@templating']
215221
216222
.. code-block:: xml
217223
218224
<!-- app/config/services.xml -->
219225
<services>
220-
<service id="app.hello_controller" class="AppBundle\Controller\HelloController">
226+
<service id="AppBundle\Controller\HelloController">
221227
<argument type="service" id="templating"/>
222228
</service>
223229
</services>
@@ -226,13 +232,10 @@ argument:
226232
227233
// app/config/services.php
228234
use AppBundle\Controller\HelloController;
229-
use Symfony\Component\DependencyInjection\Definition;
230235
use Symfony\Component\DependencyInjection\Reference;
231236
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'));
236239
237240
Rather than fetching the ``templating`` service from the container, you can
238241
inject *only* the exact service(s) that you need directly into the controller.

0 commit comments

Comments
 (0)
0