-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Comments
The difference is that the 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 // 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 |
@guillbdx up for submitting a PR? |
4.1 documentation only refer to Will the |
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 |
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 Abst 8000 ractController was too confusing for beginers. See that discussion here : #9991 (comment) |
Actually, there is a PR proposing to deprecate |
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: |
Uh oh!
There was an error while loading. Please reload this page.
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
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.
The text was updated successfully, but these errors were encountered: