8000 Difference between Controller and AbstractController · Issue #9926 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Difference between Controller and AbstractController #9926

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
guillbdx opened this issue Jun 15, 2018 · 16 comments
Closed

Difference between Controller and AbstractController #9926

guillbdx opened this issue Jun 15, 2018 · 16 comments
Labels
actionable Clear and specific issues ready for anyone to take them. good first issue Ideal for your first contribution! (some Symfony experience may be required) hasPR A Pull Request has already been submitted for this issue.
Milestone

Comments

@guillbdx
Copy link
guillbdx commented Jun 15, 2018

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.

@xabbuh
Copy link
Member
xabbuh commented Jun 15, 2018

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?

@javiereguiluz
Copy link
Member

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.)

@HeahDude
Copy link
Contributor

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.

@guillbdx
Copy link
Author

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

@javiereguiluz javiereguiluz added good first issue Ideal for your first contribution! (some Symfony experience may be required) actionable Clear and specific issues ready for anyone to take them. labels Jun 20, 2018
@nicolas-grekas
Copy link
Member
nicolas-grekas commented Jun 20, 2018
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?

@HeahDude HeahDude added this to the 3.4 milestone Jun 20, 2018
@nicolas-grekas
Copy link
Member

@guillbdx up for submitting a PR?

@guillbdx
Copy link
Author
guillbdx commented Jul 2, 2018

@nicolas-grekas #9991

@HeahDude HeahDude added the hasPR A Pull Request has already been submitted for this issue. label Jul 2, 2018
@Seb33300
Copy link
Contributor

4.1 documentation only refer to AbstractController.
Any reason?

Will the Controller class will be deprecated?

@eman1986
Copy link

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

@eman1986
Copy link

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.

@guillbdx
Copy link
Author

@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 Abst 8000 ractController was too confusing for beginers.

See that discussion here : #9991 (comment)

@nicolas-grekas
Copy link
Member

Actually, there is a PR proposing to deprecate Controller, see symfony/symfony#28243

@shashikantx
Copy link
shashikantx commented Aug 29, 2018

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

@nicolas-grekas
Copy link
Member

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

@eman1986
Copy link

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

@kpower
Copy link
kpower commented Sep 14, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable Clear and specific issues ready for anyone to take them. good first issue Ideal for your first contribution! (some Symfony experience may be required) hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

9 participants
0