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 8000

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

Conversation

guillbdx
Copy link
@guillbdx guillbdx commented Jul 2, 2018

Updates tip about difference between Controller and AbstractController.

Updates tip about difference between Controller and AbstractController.
Copy link
Contributor
@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This plays well with #9989. Thanks!

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?

@javiereguiluz
Copy link
Member

After reading Yonel's comment (#9991 (comment)) I'm starting to think that we should remove this note altogether. The controller.rst article is CRITICAL for Symfony Docs. Newcomers reading this article could think that the (false) rumors about Symfony being too complex and too academic are true (vs other frameworks which are more pragmatic).

On this introductory article, it could be better to focus on showing only AbstractController and injecting the services via the constructor or the actions ... and never mention Controller and $this->get(...) or $this->container->... Later, in the service locator article, we can explain this in detail. But mentioning all this in the critical and introductory controllers article looks very wrong to me.

@chalasr
Copy link
Member
chalasr commented Jul 3, 2018

I agree with @javiereguiluz, it's more confusing than useful.
To me, having 2 ways of doing the same thing always makes it harder to understand.
Using AbstractController (i.e. using DI in controllers) is the preferred way to create controllers since 3.4, I'd say it should be the only one to be documented.
(extending Controller is the legacy way to me, as well as any ContainerAware thing which IMHO we should deprecate in 4.x)

@guillbdx
Copy link
Author
guillbdx commented Jul 3, 2018

You're right, this tip is probably confusing for new comers. Thing is that the maker command make:controller, which is recommended in the Controller introduction article still generates a class extending Controller instead of AbstractController, which is in contradiction with best practices. But that has more to do with the maker command itself than with the documentation.

@javiereguiluz
Copy link
Member

@guillbdx yes ... we discussed about that in the Maker bundle repository. We didn't want to make the change until 4.1 was released, because AbstractController was missing the getParameter() shortcut. But now that the shortcut was added in 4.1, I think we can safely switch to AbstractController in the maker too.

@javiereguiluz
Copy link
Member

I'm closing this pull request for the reasons given above. Instead:

Thank you all for the discussion!

javiereguiluz added a commit that referenced this pull request Jul 5, 2018
…aviereguiluz)

This PR was squashed before being merged into the 4.1 branch (closes #10017).

Discussion
----------

Use AbstractController in the main controller article

This solves #9991 differently.

Commits
-------

c341381 Use AbstractController in the main controller article
javiereguiluz added a commit to symfony/maker-bundle that referenced this pull request Jul 26, 2018
…igher (javiereguiluz)

This PR was squashed before being merged into the 1.0-dev branch (closes #221).

Discussion
----------

Extend from AbstractController when using Symfony 4.1 or higher

Related to symfony/symfony-docs#9991 ... let's extend from `AbstractController` when possible.

Commits
-------

2e6403b Extend from AbstractController when using Symfony 4.1 or higher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0