FOSRestBundle: Using FOS\RestBundle\Controller\Annotations\View throws exception in Symfony 4
I’m migrating from Sf 3 to Sf4, this is how my controller looks like
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations as Rest;
... some code
/**
* @Post("/users")
* @Rest\View(statusCode=201)
*/
public function registerAction(Request $request)
{
... some action
}
And then I receive this exception:
Warning: ReflectionObject::__construct() expects parameter 1 to be object, null given in vendor/friendsofsymfony/rest-bundle/EventListener/ViewResponseListener.php line 162
This same code works on Symfony 3.3.10. Also - when I remove this - @Rest\View(statusCode=201) then everything works.
I’m using FOSRestBundle v2.3.0
I noticed that Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController is not being executed. This may be crucial as there is this line: $template->setOwner($controller); which - not called - causes error in ViewResponseListener. Maybe I have not configured something properly yet, I’ll check it.
EDIT:
Ok, so I found the cause. In Sensio\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler\OptimizerPass::process
there is this line:
if (!$container->hasDefinition('twig')) {
$container->removeDefinition('sensio_framework_extra.view.listener');
}
which I think everyone understands by now what is doing. When I commented this out - it worked. Funny thing - due to the fact that twig was by default in Sf 3.3.10 this was not the issue. But right now FOSRest depends heavily on that. Unless there is another way of doing what I’m doing 😉
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 7
- Comments: 19 (10 by maintainers)
Commits related to this issue
- minor #1834 run checks after SensioFrameworkExtraBundle (xabbuh) This PR was merged into the 2.3-dev branch. Discussion ---------- run checks after SensioFrameworkExtraBundle This does not solve t... — committed to FriendsOfSymfony/FOSRestBundle by xabbuh 6 years ago
- Require TwigBundle As per this commen's suggestion https://github.com/FriendsOfSymfony/FOSRestBundle/issues/1812#issuecomment-349930668 Twig Bundle is required to prevent a thrown exception: ``` Wa... — committed to DavidGarciaCat/FOSRestBundle by DavidGarciaCat 6 years ago
- composer require twig https://github.com/FriendsOfSymfony/FOSRestBundle/issues/1812#issuecomment-349930668 — committed to DavidGarciaCat/sf34-rest by DavidGarciaCat 6 years ago
you should add populateDefaultVars in the view annotation parameters like this :
/** * * @Rest\View(populateDefaultVars=false) */
So what this issue is about: Make ViewResponseListener work without Twig and potentially also without SensioFrameworkExtraBundle.
Hi @xabbuh
Regarding PR https://github.com/FriendsOfSymfony/FOSRestBundle/pull/1834
Quick question: given the PR description is pointing the
TwigBundleandFOSRestBundleis a Bundle to create RESTful APIs (that nowadays usually return XML or JSON) what’s the point to force me to install and enable a Bundle that deals with templates?Maybe I don’t understand the logic behind this, but it seems useless to me to throw an error just because I don’t have the
TwigBundlewhen I have no intention to manage anything related with that Bundle or templates.On the other hand, if
TwigBundleis required, it should be included as a requirement incomposer.jsonfile to make sure no one has gets an error like this, right?+1, same problem with Symfony 3.4 (installed with Symfony Flex). Installation of Twig bundle resolve the problem. But it’s ugly to have to install Twig for JSON rendered 😉