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

Document FQCN named controllers #7771

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

Closed
wants to merge 3 commits into from
Closed
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
97 changes: 82 additions & 15 deletions 97 controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,37 @@ Then you can define it as a service as follows:

# app/config/services.yml
services:
app.hello_controller:
AppBundle\Controller\HelloController:
class: AppBundle\Controller\HelloController
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class can be omitted here as well


.. code-block:: xml

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but isn't this possible too?

<service id="AppBundle\Controller\HelloController" />

Copy link
Contributor Author

@GuilhemN GuilhemN Apr 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is, but only in 3.3. I think we should keep a notation that works on every supported version as it's not the main subject.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your aim is at the master branch, hence I thought you were doing this for 3.3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything that used to work before 3.3 should probably target the respective lower branches in separate PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh you're right, I rebased this PR on 2.7.

@iltar I'll propose using the short notation in a new PR for 3.3, I should have done that since the beginning. Thanks for your comments!

Copy link
Contributor

@HeahDude HeahDude Apr 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 👎 for changing service ids in 2.x docs, or we should do it absolutely everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It adds value only for controllers as it allows to use normal notations so I don't see the need to update all service ids. Also imo if it becomes the standard with 3.3 we should also promote it for 2.x.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you call "normal notations"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With fqcn named services you can use all controller notations (e.g. AppBundle\Controller\HelloController::fooAction, App:Hello:foo, etc.). No need to define the service if you use @Route.

With other service ids you have to use the single colon notation: app.hello_controller:fooAction.

</services>

.. code-block:: php

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't something like this possible?

$container->register(HelloController::class);

I believe this also had a shortcut somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we would have an example only working on 3.3, is it ok?


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 +124,82 @@ the route ``_controller`` value:
'_controller' => 'app.hello_controller:indexAction',
)));

.. note::

You cannot drop the ``Action`` part of the method name when using this
syntax.

.. 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.

For example, you could use annotations in the ``HelloController`` defined
earlier::

// src/AppBundle/Controller/HelloController.php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class HelloController
{
/**
* @Route("/hello")
*/
public function indexAction($name)
{
// ...
}
}

With the following routes:

.. configuration-block::

.. code-block:: yaml

# app/config/routing.yml
app:
resource: "@AppBundle/Controller/"
type: annotation

.. code-block:: xml

<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<!-- the type is required to enable the annotation reader for this resource -->
<import resource="@AppBundle/Controller/" type="annotation"/>
</routes>

.. code-block:: php

// app/config/routing.php
use Symfony\Component\Routing\RouteCollection;

$collection = new RouteCollection();
$collection->addCollection(
// second argument is the type, which is required to enable
// the annotation reader for this resource
$loader->import("@AppBundle/Controller/", "annotation")
);

return $collection;

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

.. versionadded:: 2.6
Support for ``__invoke()`` was introduced in Symfony 2.6.
Expand Down Expand Up @@ -212,15 +278,15 @@ argument:

# app/config/services.yml
services:
app.hello_controller:
AppBundle\Controller\HelloController:
class: 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" class="AppBundle\Controller\HelloController">
<argument type="service" id="templating"/>
</service>
</services>
Expand All @@ -232,6 +298,7 @@ argument:
use Symfony\Component\DependencyInjection\Reference;

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

Rather than fetching the ``templating`` service from the container, you can
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.