Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[FrameworkBundle] call setContainer() for autowired controllers#23239
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
GuilhemN commentedJun 20, 2017
Isn't this missing |
jvasseur commentedJun 20, 2017
IMO it should be the role of the DI component to call the |
robfrawley commentedJun 20, 2017
Why doesn't |
nicolas-grekas commentedJun 20, 2017
Because PSR 11 isn't compatible with it. |
xabbuh commentedJun 20, 2017
The PR now handles the |
ogizanagi commentedJun 20, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Have you already tried with the DI Extra Bundle ? |
mvrhov commentedJun 21, 2017
Why do we need to inject container if the controller is autowired? If one is changing the controllers to new autowired way it should expect to remove the direct access to the container along the way. |
ogizanagi commentedJun 21, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I agree. It also feels weird injecting it at runtime in a service. In#23200 (comment) I suggested another fix allowing the |
vincentpazeller commentedJun 21, 2017
I think the container can still be useful in a controller even with autowiring. For instance autowiring cannot be used when the service to be called is only known at runtime (from the db for instance): Of course, using the container that way is not usually a good practice, but I believe that in some situations it can help reduce code size... |
xabbuh commentedJun 21, 2017
I just checked again and this is only an issue I was able to reproduce with routes registered by FOSRestBundle. The routes that are being registered then reference the controller with a pattern like |
trappar commentedJun 21, 2017
@ogizanagi I am 99% sure you're right about this not fixing the issues with the DI Extra Bundle, but that's probably something that needs to be addressed in that bundle, not here. |
stof commentedJun 21, 2017
yeah, if JMSDIExtraBundle replaces the whole resolver without reusing the Symfony implementation through decoration or inheritance, it means they have to update their bundle each time Symfony adds a new feature. This is their issue. |
ogizanagi commentedJun 21, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
But the same goes for fos rest: is it normal to use the single colon notation usually used for controller as services ? |
stof commentedJun 21, 2017
@ogizanagi IIRC, maybe they wanted to define the route for container as a service. |
xabbuh commentedJun 21, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@ogizanagi Actually it's one colon for services and two for controller classes that must be instantiated by the controller resolver. |
stof commentedJun 21, 2017
@xabbuh no. 2 colons is not for services. It is for normal callables (as it is the syntax used for callables in PHP) |
xabbuh commentedJun 21, 2017
seeFriendsOfSymfony/FOSRestBundle#1721 whichcould also be a fix However, I still think that the main inconsistency is that the |
xabbuh commentedJun 21, 2017
@stof good catch, I fixed my comment accordingly |
GuilhemN left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think this is the right fix.ContainerAwareInterface is here for something,setContainer must be called by the resolver when it is detected like what does the serializer when detectingNormalizerAwareInterface for instance.
| returnparent::createController($controller); | ||
| $resolvedController =parent::createController($controller); | ||
| if (1 ===substr_count($controller,':') &&is_array($resolvedController)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
shouldn't this be done in the parent class instead, which is the one supporting container injection ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
IMO that would be inconsistent as the parent class isn't doing this for instances it creates itself either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
hmm indeed. I missed the fact that injection was done in this class in a separate method
stof commentedJul 3, 2017
hmm, actually, can we have a test covering this case to prevent regressions ? |
xabbuh commentedJul 3, 2017
I'll check |
xabbuh commentedJul 3, 2017
tests added |
nicolas-grekas commentedJul 3, 2017
Thank you@xabbuh. |
…llers (xabbuh)This PR was merged into the 3.3 branch.Discussion----------[FrameworkBundle] call setContainer() for autowired controllers| Q | A| ------------- | ---| Branch? | 3.3| Bug fix? | yes| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#23200,FriendsOfSymfony/FOSRestBundle#1719| License | MIT| Doc PR |Previously, you either did not use controllers as services or you explicitly wired everything yourself. In case of a non-service controller the FrameworkBundle took care of calling `setContainer()` inside the `instantiateController()` method:```phpprotected function instantiateController($class){ $controller = parent::instantiateController($class); if ($controller instanceof ContainerAwareInterface) { $controller->setContainer($this->container); } if ($controller instanceof AbstractController && null !== $previousContainer = $controller->setContainer($this->container)) { $controller->setContainer($previousContainer); } return $controller;}```With the new autowired controllers as services this is no longer happening as controllers do not need to be instantiated anymore (the container already returns fully built objects).Commits-------1d07a28 call setContainer() for autowired controllers
…nerAwareInterface (fixes#1666)"This reverts commitfaccea6.Fixed insymfony/symfony#23239 (Symfony 3.3.3)
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Upgrade symfony to 3.3.5 to fix the bug:symfony/symfony#23239
Uh oh!
There was an error while loading.Please reload this page.
Previously, you either did not use controllers as services or you explicitly wired everything yourself. In case of a non-service controller the FrameworkBundle took care of calling
setContainer()inside theinstantiateController()method:With the new autowired controllers as services this is no longer happening as controllers do not need to be instantiated anymore (the container already returns fully built objects).