-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[FrameworkBundle] Document the AbstractController #7657
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,19 +116,29 @@ For more information on routing, see :doc:`/routing`. | |
.. index:: | ||
single: Controller; Base controller class | ||
|
||
The Base Controller Class & Services | ||
------------------------------------ | ||
.. _anchor-name: | ||
:ref:`The Base Controller Classes & Services <the-base-controller-class-services>` | ||
|
||
For convenience, Symfony comes with an optional base | ||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class. | ||
If you extend it, this won't change anything about how your controller | ||
works, but you'll get access to a number of **helper methods** and the | ||
**service container** (see :ref:`controller-accessing-services`): an | ||
The Base Controller Classes & Services | ||
-------------------------------------- | ||
|
||
For convenience, Symfony comes with two optional base | ||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` and | ||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController` | ||
classes. | ||
If you extend one or the other, this won't change anything about how your | ||
controller works, but you'll get access to a number of **helper methods**. | ||
|
||
The base ``Controller`` also allows you to access the **service container** (see :ref:`controller-accessing-services`): an | ||
array-like object that gives you access to every useful object in the | ||
system. These useful objects are called **services**, and Symfony ships | ||
with a service object that can render Twig templates, another that can | ||
log messages and many more. | ||
|
||
On the other hand, the ``AbstractController`` prevents you from accessing the | ||
**service container**. This forces you to write a code more robust by | ||
forcing you to explicitly define your dependencies. | ||
|
||
Add the ``use`` statement atop the ``Controller`` class and then modify | ||
``LuckyController`` to extend it:: | ||
|
||
|
@@ -144,7 +154,7 @@ Add the ``use`` statement atop the ``Controller`` class and then modify | |
|
||
Helper methods are just shortcuts to using core Symfony functionality | ||
that's available to you with or without the use of the base | ||
``Controller`` class. A great way to see the core functionality in | ||
controller classes. A great way to see the core functionality in | ||
action is to look in the | ||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class. | ||
|
||
|
@@ -241,10 +251,9 @@ are used for rendering templates, sending emails, querying the database and | |
any other "work" you can think of. When you install a new bundle, it probably | ||
brings in even *more* services. | ||
|
||
When extending the base controller class, you can access any Symfony service | ||
When extending the base ``Controller`` class, you can access any Symfony service | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're missing pros of using the new AbstractController, dependencies are explicit either in a constructor or in action signatures, but the controller needs to be configured to get injection. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean we have to tell that the controller has to be configured as a service ? I updated the text to reflect that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm it seems I got confused, reading @nicolas-grekas's comment above:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I just said is wrong, the getters has nothing to do with action injections, so I don't get it, this must be configured somehow, it's just already done by the bundle. |
||
via the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::get` | ||
method of the ``Controller`` class. Here are several common services you might | ||
need:: | ||
method. Here are several common services you might need:: | ||
|
||
$templating = $this->get('templating'); | ||
|
||
|
@@ -279,7 +288,8 @@ Managing Errors and 404 Pages | |
|
||
When things are not found, you should play well with the HTTP protocol and | ||
return a 404 response. To do this, you'll throw a special type of exception. | ||
If you're extending the base ``Controller`` class, do the following:: | ||
If you're extending the base ``Controller`` or the base ``AbstractController`` | ||
class, do the following:: | ||
|
||
public function indexAction() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should add a label for the old headline to not break any deep links
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I'm not 100% sure this is how it works though.