8000 Controller vs AbstractController difference by guillbdx · Pull Request #9991 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Controller vs AbstractController difference #9991

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
Changes from 1 commit
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
Prev Previous commit
Reword
  • Loading branch information
javiereguiluz authored Jul 2, 2018
commit be2032288f3c3b986c2004e9c5ae8ca17d0a70f6
15 changes: 7 additions & 8 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,13 @@ and many others that you'll learn about next.

.. tip::

What's the difference between ``Controller`` or ``AbstractController``? Both
provide the same helper methods, but ``AbstractController`` is more strict: it
forces you to explicitly declare your dependencies before you can fetch them using
``$this->get()`` or ``$this->container->get()``. This is considered a best practice
as ``Controller`` requires services to be public to do the same. But if you do
need direct access to the container, using Controller is fine. Learn more about
how to subscribe your services in the
:doc:`/service_container/service_subscribers_locators` article.
You can extend either ``Controller`` or ``AbstractController``. The
difference is that ``AbstractController`` is more strict and doesn't let you
access services via ``$this->get()`` or ``$this->container->get()``.
Copy link
Member
@yceruto yceruto Jul 2, 2018

Choose a reason for hiding this comment

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

and doesn't let you access services via $this->get() or $this->container->get().

Except for services defined in getSubscribedServices() method.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed. How can we improve this?

Instead, you must declare your dependencies explicitly with
:doc:`service locators </service_container/service_subscribers_locators>`.
Using ``AbstractController`` is considered a best practice but if you do
need direct access to the container, using ``Controller`` is fine.

.. versionadded:: 3.3
The ``AbstractController`` class was added in Symfony 3.3.
Expand Down
0