Symfony-docs: Difference between Controller and AbstractController

Created on 15 Jun 2018  路  16Comments  路  Source: symfony/symfony-docs

Hi, i'm wondering if there is an error on this documentation page :
https://symfony.com/doc/current/controller.html#the-base-controller-classes-services

What's the difference between Controller or AbstractController? Not much: both are identical, except that AbstractController is more restrictive: it does not allow you to access services directly via $this->get() or $this->container->get().

Actually, both Controller and AbstractController use the ControllerTrait, which implements the protected method get(string $id) and has the protected $container property. So $this->get() and $this->container->get() are usable in a controller extending AbstractController as well as Controller.

actionable good first issue hasPR

Most helpful comment

What about this?

Original:

What's the difference between Controller or AbstractController? Not much: both
are identical, except that AbstractController is more restrictive: it does not
allow you to access services directly via ``$this->get()`` or
``$this->container->get()``.

Proposal:

What's the difference between Controller or AbstractController? Not much: both
are identical, except that AbstractController is more restrictive. The
``$this->get()`` and ``$this->container->get()`` methods don't allow you to
access to your own services but only to a limited set of services commonly used
in controllers (such as ``twig``, ``doctrine``, ``session``, etc.)

All 16 comments

The difference is that the AbstractController only uses a limited container that only contains some services that the controller has been subscribed to. There is just a small set of subscribed services by default: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php#L74-L92

Do you have an idea how we could clarify the difference?

What about this?

Original:

What's the difference between Controller or AbstractController? Not much: both
are identical, except that AbstractController is more restrictive: it does not
allow you to access services directly via ``$this->get()`` or
``$this->container->get()``.

Proposal:

What's the difference between Controller or AbstractController? Not much: both
are identical, except that AbstractController is more restrictive. The
``$this->get()`` and ``$this->container->get()`` methods don't allow you to
access to your own services but only to a limited set of services commonly used
in controllers (such as ``twig``, ``doctrine``, ``session``, etc.)

It should also be recommended to override the AbstractController services subscriptions.
For example when using services only when the form is submitted, one should declare its own service overriding getSubscribedServices():

// src/Controller/FooController
public function index(SomeRequiredService $service)
{
    $form = $this->createForm(FooType::class, null, ['some_option' => $service])
        ->handleRequest($request)
    ;

    if ($form->isSubmitted() && $form->isValid()) {
        $this->get(SomeOptionalService::class)->bar($form->getData());

        return $this->redirectToRoute('foo');
    }

    return $this->render('foo.html.twig', ['foo_form' => $form->createView()]);
}

public static function getSubscribedServices(): array
{
    return array_merge(parent::getSubscribedServices(), [
        SomeOptionalService::class,
    ]);
}

I don't know if this should be part of the same change, but this is more explicit about differences when using them, not just about what they are.

Ok, thanks for clarifying the difference. Javier's proposal seems very clear to me.

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.

etc. with link to doc about ServiceSubscriberInterface?

@guillbdx up for submitting a PR?

4.1 documentation only refer to AbstractController.
Any reason?

Will the Controller class will be deprecated?

I noticed that too, that'd be good to know as I use Controller at the moment so I'd like to know if I should switch to AbstractController

looking at the documentation more, wondering if Symfony is pushing for dependency injection going forward, I'm not against that at all, I've been using DI anyway as I prefer that.

@Seb33300 @eman1986 Controller will not be deprecated as far as i know, but it has been decided to remove this note about Controller on this documentation page because this page is intented to Symfony newcomers. Explaining difference between Controller and AbstractController was too confusing for beginers.

See that discussion here : https://github.com/symfony/symfony-docs/pull/9991#issuecomment-401800401

Actually, there is a PR proposing to deprecate Controller, see https://github.com/symfony/symfony/pull/28243

But why? Is there any issue with using Controller? @nicolas-grekas

See above, I've nothing else to add, that's enough a reason to me...

I don't have an issue switching to AbstractController, I realized its a better fit for how I was using it anyhow

One more thing to mention: bin/console make:controller will generate Controller's subclass. I think it should be changed to AbstractController.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samjarrett picture samjarrett  路  4Comments

javiereguiluz picture javiereguiluz  路  4Comments

steevanb picture steevanb  路  4Comments

javiereguiluz picture javiereguiluz  路  5Comments

javiereguiluz picture javiereguiluz  路  4Comments