10000 minor #10017 Use AbstractController in the main controller article (j… · symfony/symfony-docs@4094d45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4094d45

Browse files
committed
minor #10017 Use AbstractController in the main controller article (javiereguiluz)
This PR was squashed before being merged into the 4.1 branch (closes #10017). Discussion ---------- Use AbstractController in the main controller article This solves #9991 differently. Commits ------- c341381 Use AbstractController in the main controller article
2 parents b484c8b + c341381 commit 4094d45

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

controller.rst

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ For more information on routing, see :doc:`/routing`.
8686
single: Controller; Base controller class
8787

8888
.. _the-base-controller-class-services:
89+
.. _the-base-controller-classes-services:
8990

90-
The Base Controller Classes & Services
91-
--------------------------------------
91+
The Base Controller Class & Services
92+
------------------------------------
9293

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

9898
Add the ``use`` statement atop your controller class and then modify
9999
``LuckyController`` to extend it:
@@ -103,35 +103,24 @@ Add the ``use`` statement atop your controller class and then modify
103103
// src/Controller/LuckyController.php
104104
namespace App\Controller;
105105
106-
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
106+
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
107107
108108
- class LuckyController
109-
+ class LuckyController extends Controller
109+
+ class LuckyController extends AbstractController
110110
{
111111
// ...
112112
}
113113
114114
That's it! You now have access to methods like :ref:`$this->render() <controller-rendering-templates>`
115115
and many others that you'll learn about next.
116116

117-
.. _controller-abstract-versus-controller:
118-
119-
.. tip::
120-
121-
What's the difference between ``Controller`` or ``AbstractController``? Not much:
122-
both are identical, except that ``AbstractController`` is more restrictive: it
123-
does not allow you to access services directly via ``$this->get()`` or
124-
``$this->container->get()``. This forces you to write more robust code to access
125-
services. But if you *do* need direct access to the container, using ``Controller``
126-
is fine.
127-
128117
.. index::
129118
single: Controller; Redirecting
130119

131120
Generating URLs
132121
~~~~~~~~~~~~~~~
133122

134-
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::generateUrl`
123+
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait::generateUrl`
135124
method is just a helper method that generates the URL for a given route::
136125

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

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

656645
In other articles, you'll learn how to use specific services from inside your controller

controller/service.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ without the method (``App\Controller\HelloController`` for example).
8181
Alternatives to base Controller Methods
8282
---------------------------------------
8383

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

8989
The base `Controller class source code`_ is a great way to see how to accomplish

service_container/3.3-di-changes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ The third big change is that, in a 658B new Symfony 3.3 project, your controllers are
326326
$this->registerClasses($definition, 'App\\Controller\\', '../src/Controller/*');
327327
328328
But, you might not even notice this. First, your controllers *can* still extend
329-
the same base ``Controller`` class or a new :ref:`AbstractController <controller-abstract-versus-controller>`.
329+
the same base controller class (``AbstractController``).
330330
This means you have access to all of the same shortcuts as before. Additionally,
331331
the ``@Route`` annotation and ``_controller`` syntax (e.g. ``App:Default:homepage``)
332332
used in routing will automatically use your controller as a service (as long as its

0 commit comments

Comments
 (0)
0