Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
nicolas-grekas merged 1 commit intosymfony:3.3fromxabbuh:issue-23200
Jul 3, 2017

Conversation

@xabbuh
Copy link
Member

@xabbuhxabbuh commentedJun 20, 2017
edited
Loading

QA
Branch?3.3
Bug fix?yes
New feature?no
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#23200,FriendsOfSymfony/FOSRestBundle#1719
LicenseMIT
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 callingsetContainer() inside theinstantiateController() method:

protectedfunctioninstantiateController($class){$controller =parent::instantiateController($class);if ($controllerinstanceof ContainerAwareInterface) {$controller->setContainer($this->container);    }if ($controllerinstanceof 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).

thibaultfalque, addiks, GabeAtWork, and ivanoffbg reacted with thumbs up emoji
@GuilhemN
Copy link
Contributor

Isn't this missingAbstractController support?

chalasr and yceruto reacted with thumbs up emoji

@jvasseur
Copy link
Contributor

IMO it should be the role of the DI component to call thesetContainer method, maybe by adding this magic method call behind theautoconfigure option.

ogizanagi reacted with thumbs up emoji

@robfrawley
Copy link
Contributor

Why doesn'tAbstractController implementContainerAwareInterface?

@nicolas-grekas
Copy link
Member

Because PSR 11 isn't compatible with it.

robfrawley, Hanmac, and emirb reacted with thumbs up emoji

@xabbuh
Copy link
MemberAuthor

The PR now handles theAbstractController class too now.

@ogizanagi
Copy link
Contributor

ogizanagi commentedJun 20, 2017
edited
Loading

Have you already tried with the DI Extra Bundle ?
I doubt it'll work (for those using the bundle. I don't really get other broken cases), unless I missed something, as itreplaces the controller resolver used and theimplementation does not make use of the parent. Perhaps@bogdaniel or@trappar could confirm.

@mvrhov
Copy link

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.
IMO it this should be marked as deprecated since 3.4 and removed in 4.0

@ogizanagi
Copy link
Contributor

ogizanagi commentedJun 21, 2017
edited
Loading

I agree. It also feels weird injecting it at runtime in a service. In#23200 (comment) I suggested another fix allowing thesetContainer call to be properly autowired, which I guess should also work for those using the JMSDIExtraBundle. Having a controller as service with the whole container injection kind of defeat the purpose of defining it as a service. Still, there are real issues which needs to be fixed, as proven in the related issues.

@vincentpazeller
Copy link

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

public function myAction(){$dynamicServiceName = ... $dynamicService = $this->container->get($dynamicServiceName)...}

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
Copy link
MemberAuthor

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 likeAppBundle\Controller\UserController:getAction while routes registered with Symfony's annotation loader have two colons (and only in this caseinstantiateController() will be called and thus injecting the container).

@trappar
Copy link

@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
Copy link
Member

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.

clubdesarrolladores and lenybernard reacted with thumbs up emoji

@ogizanagi
Copy link
Contributor

ogizanagi commentedJun 21, 2017
edited
Loading

But the same goes for fos rest: is it normal to use the single colon notation usually used for controller as services ?

@stof
Copy link
Member

@ogizanagi IIRC, maybe they wanted to define the route for container as a service.

@xabbuh
Copy link
MemberAuthor

xabbuh commentedJun 21, 2017
edited
Loading

@ogizanagi Actually it's one colon for services and two for controller classes that must be instantiated by the controller resolver.

@stof
Copy link
Member

@xabbuh no. 2 colons is not for services. It is for normal callables (as it is the syntax used for callables in PHP)

@xabbuh
Copy link
MemberAuthor

seeFriendsOfSymfony/FOSRestBundle#1721 whichcould also be a fix

However, I still think that the main inconsistency is that theControllerResolver from the FrameworkBundle is sometimes callingsetContainer() even if the controller is registered as a service, but that whether or not this is done depends on the notation of the_controller attribute.

@xabbuh
Copy link
MemberAuthor

@stof good catch, I fixed my comment accordingly

Copy link
Contributor

@GuilhemNGuilhemN left a 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.

@nicolas-grekasnicolas-grekas added this to the3.3 milestoneJul 3, 2017
returnparent::createController($controller);
$resolvedController =parent::createController($controller);

if (1 ===substr_count($controller,':') &&is_array($resolvedController)) {
Copy link
Member

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 ?

Copy link
MemberAuthor

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.

Copy link
Member

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
Copy link
Member

stof commentedJul 3, 2017

hmm, actually, can we have a test covering this case to prevent regressions ?

@xabbuh
Copy link
MemberAuthor

I'll check

@xabbuh
Copy link
MemberAuthor

tests added

@nicolas-grekas
Copy link
Member

Thank you@xabbuh.

@nicolas-grekasnicolas-grekas merged commit1d07a28 intosymfony:3.3Jul 3, 2017
nicolas-grekas added a commit that referenced this pull requestJul 3, 2017
…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
@xabbuhxabbuh deleted the issue-23200 branchJuly 3, 2017 14:52
@fabpotfabpot mentioned this pull requestJul 4, 2017
maff added a commit to pimcore/pimcore that referenced this pull requestJul 4, 2017
…nerAwareInterface (fixes#1666)"This reverts commitfaccea6.Fixed insymfony/symfony#23239 (Symfony 3.3.3)
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestJul 26, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestJul 26, 2017
aquadran pushed a commit to IoTAqua/supla-cloud that referenced this pull requestAug 16, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 17, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 17, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 18, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 18, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 18, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 18, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 18, 2017
fracz added a commit to SUPLA/supla-cloud that referenced this pull requestAug 18, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof approved these changes

+1 more reviewer

@GuilhemNGuilhemNGuilhemN approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

11 participants

@xabbuh@GuilhemN@jvasseur@robfrawley@nicolas-grekas@ogizanagi@mvrhov@vincentpazeller@trappar@stof@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp