-
Notifications
You must be signed in to change notification settings - Fork 703
-
Notifications
You must be signed in to change notification settings - Fork 703
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
Container not injected in controllers with FosRest 2.2 and Symfony 3.3 in rest routes #1719
Comments
Are you able to set up a small example project that makes it possible to reproduce your issue? |
I have the same problem with Symfony 3.3.2. $this->container is null. So I get: |
Maybe related to symfony/symfony#23200? Do you make use of the new feature that makes it possible to easily register controllers as services? |
Yes it might be related.
and it works. But it's obvious that something wrong with auto injecting container in controller. |
Do you use JMSDiExtraBundle? |
I don't. Here is my composer.json deps:
|
Do you register your controllers as services? If yes, what does your config look like? Can you also show the code of your controller? |
This is the controller I stuck with:
My services.yml:
|
I got the same issue, it is a pain in the ass |
Can someone of you create a small example project that makes it easily possible to reproduce your issue? |
@xabbuh Here is a test project with the problem: But it use db. You need configure and create some db. And run migrations:
Actually it creates FOSUserBundle User table. |
Thank you for providing this useful example. I think I was able to reproduce the issue. Can you please check if symfony/symfony#23239 fixes it for you? |
@xabbuh Yes, it fixes the issue. Container is in controller now. |
Hi, Thanks. |
to fix it you have to update Framework Bundle... or just edit it and add the new content https://github.com/symfony/symfony/pull/23239/files vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php |
Will it be fixed somehow in the bundle or should we wait for the next symfony release and until then apply some custom fixes? |
…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: ```php protected 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
symfony/symfony#23239 now merged |
…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: ```php protected 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
Symfony 3.3.3 containing this fix was released. |
Symfony 3.3.9 has this issue when using JMSDiExtraBundle. The "solution" (I bracket it as I have NO idea whats going on) was to add the setContainer call to my controllers in the "generic" autowiring section:
|
Also Symfony 3.3.13 is still affected. The solution is the same above... but didn't have it been fixed already? |
Are you using JMSDiExtraBundle? |
Yes, version 1.9.0 |
Then it's probably related to their code. I don't know if they ever fixed the issue on their side. |
@ventuc you need to downgrade JMSDiExtraBundle to 1.7.0 version. |
When trying to set up a route for my rest api, when I visit my "/places" route I get a FatalThrowableError: "Call to a member function has() on null" in vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait.php (line 356)
The problem only happens when I switch my routes from annotation to rest.
This doesn't work:
This works:
The method is almost empty, only a call to getDoctrine:
And the container is null. I checked and the setContainer method is never called on my Controller and I can't find why.
The same thing works with identical configuration with Symfony 3.2.9, but not with Symfony 3.3.
The text was updated successfully, but these errors were encountered: