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 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
12 changes: 7 additions & 5 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ and many others that you'll learn about next.

.. tip::

You can extend either ``Controller`` or ``AbstractController``. The difference
is that when you extend ``AbstractController``, you can't 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.
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