Skip to content

Controller as service example without deprecated FrameworkExtraBundle #9595

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 1 commit into from
Apr 16, 2018
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
23 changes: 12 additions & 11 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ knows to use it.

Use the ``service_id::method_name`` syntax to refer to the controller method.
If the service id is the fully-qualified class name (FQCN) of your controller,
as Symfony recommends, then you can use something like:
``App\Controller\HelloController::index``:
as Symfony recommends, then the syntax is the same as if the controller was not
a service like: ``App\Controller\HelloController::index``:

.. configuration-block::

.. code-block:: php-annotations

// src/Controller/HelloController.php

// You need to use Sensio's annotation to specify a service id
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
// ...
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route(service="App\Controller\HelloController::index")
*/
class HelloController
{
// ...
/**
* @Route("/hello", name="hello")
*/
public function index()
{
// ...
}
}

.. code-block:: yaml
Expand Down Expand Up @@ -75,7 +76,7 @@ Invokable Controllers

If your controller implements the ``__invoke()`` method - popular with the
Action-Domain-Response (ADR) pattern, you can simply refer to the service id
(``App\Controller\HelloController`` for example).
without the method (``App\Controller\HelloController`` for example).

Alternatives to base Controller Methods
---------------------------------------
Expand All @@ -89,7 +90,7 @@ The base `Controller class source code`_ is a great way to see how to accomplish
common tasks. For example, ``$this->render()`` is usually used to render a Twig
template and return a Response. But, you can also do this directly:

In a controller that's defined as a service, you can instead inject the ``templating``
In a controller that's defined as a service, you can instead inject the ``twig``
service and use it directly::

// src/Controller/HelloController.php
Expand Down