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
13 changes: 8 additions & 5 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,14 @@ 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.
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.

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