2

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 ?

Community
  • 1
  • 1
Bonswouar
  • 1,521
  • 2
  • 17
  • 37
  • should probably be `$kernel->getContainer()->set('request', Request::createFromGlobals(), 'request');` otherwise why are you creating the `$request` variable ? – Nicolai Fröhlich Feb 10 '14 at 21:12
  • Yes that was a mistake, but it actually throws the same error doing that.. – Bonswouar Feb 11 '14 at 08:18
  • 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 Feb 11 '14 at 09:51
  • Did you find a solution? I have been using the same snippet for a long time and it is not working now. – Callistino Feb 25 '14 at 13:44
  • 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 Feb 25 '14 at 13:51

2 Answers2

6

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

 $this->getContainer()->get('request_stack')->push(Request::createFromGlobals());
da2ky
  • 81
  • 1
  • 3
0

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
antila
  • 364
  • 1
  • 4
  • I have tried the same, but it doesn't solve my problem. Although I am using a Symfony 2.7, but I have rebuilt the same bootstrap. – FortuneSoldier Feb 20 '18 at 08:04