8000 [3.3] Document FQCN named controllers by GuilhemN · Pull Request #7864 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[3.3] Document FQCN named controllers #7864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,36 @@ Then you can define it as a service as follows:

# app/config/services.yml
services:
app.hello_controller:
class: AppBundle\Controller\HelloController
AppBundle\Controller\HelloController: ~

.. code-block:: xml

<!-- app/config/services.xml -->
<services>
<service id="app.hello_controller" class="AppBundle\Controller\HelloController" />
<service id="AppBundle\Controller\HelloController" />
</services>

.. code-block:: php

// app/config/services.php
use AppBundle\Controller\HelloController;

$container->register('app.hello_controller', HelloController::class);
$container->register(HelloController::class);

Referring to the Service
------------------------

To refer to a controller that's defined as a service, use the single colon (:)
notation. For example, to forward to the ``indexAction()`` method of the service
defined above with the id ``app.hello_controller``::
If the service id is the fully-qualified class name (FQCN) of your controller,
you can keep using the usual notation. For example, to forward to the
``indexAction()`` method of the above ``AppBundle\Controller\HelloController``
service::

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

.. note::
Otherwise, use the single colon (``:``) notation. For example, to forward to the
``indexAction()`` method of a service with the id ``app.hello_controller``::

You cannot drop the ``Action`` part of the method name when using this
syntax.
$this->forward('app.hello_controller:indexAction', array('name' => $name));

You can also route to the service by using the same notation when defining
the route ``_controller`` value:
Expand Down Expand Up @@ -123,17 +123,24 @@ the route ``_controller`` value:
'_controller' => 'app.hello_controller:indexAction',
)));

.. note::

You cannot drop the ``Action`` part of the method name when using the
single colon notation.

.. tip::

You can also use annotations to configure routing using a controller
defined as a service. Make sure you specify the service ID in the
``@Route`` annotation. See the `FrameworkExtraBundle documentation`_ for
details.
``@Route`` annotation if your service ID is not your controller
fully-qualified class name (FQCN). See the
`FrameworkExtraBundle documentation`_ for details.

.. tip::

If your controller implements the ``__invoke()`` method, you can simply
refer to the service id (``app.hello_controller``).
refer to the service id (``AppBundle\Controller\HelloController`` or
``app.hello_controller`` for example).

Alternatives to base Controller Methods
---------------------------------------
Expand Down Expand Up @@ -209,15 +216,14 @@ argument:

# app/config/services.yml
services:
app.hello_controller:
class: AppBundle\Controller\HelloController
AppBundle\Controller\HelloController:
arguments: ['@templating']

.. code-block:: xml

<!-- app/config/services.xml -->
<services>
<service id="app.hello_controller" class="AppBundle\Controller\HelloController">
<service id="AppBundle\Controller\HelloController">
<argument type="service" id="templating"/>
</service>
</services>
Expand All @@ -226,13 +232,10 @@ argument:

// app/config/services.php
use AppBundle\Controller\HelloController;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$container->setDefinition('app.hello_controller', new Definition(
HelloController::class,
array(new Reference('templating'))
));
$container->register(HelloController::class)
->addArgument(new Reference('templating'));

Rather than fetching the ``templating`` service from the container, you can
inject *only* the exact service(s) that you need directly into the controller.
Expand Down
0