Skip to content

Use AbstractController in the main controller article #10017

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 2 commits into from
Closed
Show file tree
Hide file tree
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
29 changes: 9 additions & 20 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ For more information on routing, see :doc:`/routing`.
single: Controller; Base controller class

.. _the-base-controller-class-services:
.. _the-base-controller-classes-services:

The Base Controller Classes & Services
--------------------------------------
The Base Controller Class & Services
------------------------------------

To make life nicer, Symfony comes with two optional base
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` and
To make life nicer, Symfony comes with an optional base controller class called
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`.
You can extend either to get access to some `helper methods`_.
You can extend it to get access to some `helper methods`_.

Add the ``use`` statement atop your controller class and then modify
``LuckyController`` to extend it:
Expand All @@ -103,35 +103,24 @@ Add the ``use`` statement atop your controller class and then modify
// src/Controller/LuckyController.php
namespace App\Controller;

+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

- class LuckyController
+ class LuckyController extends Controller
+ class LuckyController extends AbstractController
{
// ...
}

That's it! You now have access to methods like :ref:`$this->render() <controller-rendering-templates>`
and many others that you'll learn about next.

.. _controller-abstract-versus-controller:

.. tip::

What's the difference between ``Controller`` or ``AbstractController``? Not much:
both are identical, except that ``AbstractController`` is more restrictive: it
does not allow you to access services directly via ``$this->get()`` or
``$this->container->get()``. This forces you to write more robust code to access
services. But if you *do* need direct access to the container, using ``Controller``
is fine.

.. index::
single: Controller; Redirecting

Generating URLs
~~~~~~~~~~~~~~~

The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::generateUrl`
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait::generateUrl`
method is just a helper method that generates the URL for a given route::

$url = $this->generateUrl('app_lucky_number', array('max' => 10));
Expand Down Expand Up @@ -650,7 +639,7 @@ contains the logic for that page. In Symfony, this is called a controller,
and it's a PHP function where you can do anything in order to return the
final ``Response`` object that will be returned to the user.

To make life easier, you'll probably extend the base ``Controller`` class because
To make life easier, you'll probably extend the base ``AbstractController`` class because
this gives access to shortcut methods (like ``render()`` and ``redirectToRoute()``).

In other articles, you'll learn how to use specific services from inside your controller
Expand Down
6 changes: 3 additions & 3 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ without the method (``App\Controller\HelloController`` for example).
Alternatives to base Controller Methods
---------------------------------------

When using a controller defined as a service, you can still extend any of the
:ref:`normal base controller <the-base-controller-class-services>` classes and
use their shortcuts. But, you don't need to! You can choose to extend *nothing*,
When using a controller defined as a service, you can still extend the
:ref:`AbstractController base controller <the-base-controller-class-services>`
and use its shortcuts. But, you don't need to! You can choose to extend *nothing*,
and use dependency injection to access different services.

The base `Controller class source code`_ is a great way to see how to accomplish
Expand Down
2 changes: 1 addition & 1 deletion service_container/3.3-di-changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
$this->registerClasses($definition, 'App\\Controller\\', '../src/Controller/*');

But, you might not even notice this. First, your controllers *can* still extend
the same base ``Controller`` class or a new :ref:`AbstractController <controller-abstract-versus-controller>`.
the same base controller class (``AbstractController``).
This means you have access to all of the same shortcuts as before. Additionally,
the ``@Route`` annotation and ``_controller`` syntax (e.g. ``App:Default:homepage``)
used in routing will automatically use your controller as a service (as long as its
Expand Down