2
votes

Since I migrated to Symfony 2.4, I'm getting the following error message :

Rendering a fragment can only be done when handling a Request.

It's happening because, on some pages, I'm rendering some templates with Twig inside some pages which are handled by another older framework, by doing $sf2->container->get('twig')->render("MyBundle::my-template.html.twig");.

So yes, that's right that Symfony 2 isn't handling those requests, but I still want to render those templates with Twig ! Why can't I do that (anymore) ?! And how to fix that ?

Here is the code I'm executing to "boot" SF2 in my older project :

$loader = require_once __DIR__.'/../../../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../../../app/AppKernel.php';

$kernel = new AppKernel('bootstrap', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$kernel->boot();
$kernel->getContainer()->enterScope('request');
$kernel->getContainer()->set('request', $request, 'request');
$this->container = $kernel->getContainer();

EDIT : By the way, it might be related to that : Symfony 2.4 Rendering a controller in TWIG throws "Rendering a fragment can only be done when handling a Request." Exception . Though, I don't want to downgrade to Symfony 2.3, and deleting the vendor directory didn't fix my problem.

EDIT² : I found out that the problem is because of the new RequestStack.

In HttpKernel, handleRaw(Request $request, $type = self::MASTER_REQUEST) normally push the request to the RequestStack ($this->requestStack->push($request);). So if I add a public method pushRequestStack($request) to the HttpKernel it works.. But how could I do it properly ? I don't find any public method able to get the $requestStack from HttpKernel (so I can push the request externally)..

And I can't use the "normal" method ($kernel->handle($request)) because it would throw some exceptions, for example for the route that doesn't exist, or also for the session that has already been started by PHP..

In conclusion, is there any way to "push" my/any request to the requestStack without completly handling the request ?

2
should probably be $kernel->getContainer()->set('request', Request::createFromGlobals(), 'request'); otherwise why are you creating the $request variable ?Nicolai Fröhlich
Yes that was a mistake, but it actually throws the same error doing that..Bonswouar
I just updated my original post, now I know where the problem is from, I just don't know how to get round it !Bonswouar
Did you find a solution? I have been using the same snippet for a long time and it is not working now.Callistino
No I didn't. Actually, instead of wasting too much time looking for a solution, I chose to use only the twig template rendering from SF2 in my older framework.. But I don't even have access to path() or render().Bonswouar

2 Answers

6
votes

You have to push a new Request in the "request_stack" from your Sf2 command.

 $this->getContainer()->get('request_stack')->push(Request::createFromGlobals());
0
votes

I had the same problem and solved this by rebuilding the bootstrap with this command:

php vendor/bundles/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php app