2
votes

After upgrading from Symfony 2.3 to 2.4, the following exception is being thrown:

An exception has been thrown during the rendering of a template ("Rendering a fragment can only be done when handling a Request.") in PageBundle::base.html.twig at line 34.

The code is trying to render a controller:

{% if not app.user %}
<div id="login" class="fourcol last">
    {{ render(controller("SecurityBundle:Front/Security:login")) }}
</div>
{% endif %}

The exception is being thrown by fragmentHandler because $request is null:

public function render($uri, $renderer = 'inline', array $options = array())
{
    if (!isset($options['ignore_errors'])) {
        $options['ignore_errors'] = !$this->debug;
    }

    if (!isset($this->renderers[$renderer])) {
        throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
    }

    if (!$request = $this->getRequest()) {
        throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
    }
var_dump($request); die();
    return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
} 

This is also happening on other twig render function calls.

List of Symfony component versions from composer:

symfony/assetic-bundle               v2.3.0
symfony/icu                          v1.2.0
symfony/monolog-bundle               v2.3.0
symfony/swiftmailer-bundle           v2.3.4
symfony/symfony                      v2.4.0 

Any help would be greatly appreciated

3

3 Answers

1
votes

i have the same error, but if i delete the "vendor" folder and reinit it via "php composer.phar install", all works fine...

0
votes

Luckily I had a test version of the site deployed elsewhere so I was able to compare the dependency versions. This error was solved by reverting back to Symfony 2.3.7 Here's the updated versions:

symfony/assetic-bundle               v2.3.0
symfony/icu                          v1.2.0 
symfony/monolog-bundle               v2.3.0 
symfony/swiftmailer-bundle           v2.3.4
symfony/symfony                      v2.3.7 

The render method in the FragmentHandler in 2.3.7 has a different condition from 2.4.0

 if (null === $this->request) {
        throw new \LogicException('Rendering a fragment can only be done when handling a master Request.');
 }

This is checking for null

0
votes

Not sure I had the same problem, but it gave me the "Rendering a fragment can only be done when handling a Request." error.

I solved that by rebuilding the bootstrap with this command:

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

Also see: Updating Symfony 2.4 : “Rendering a fragment can only be done when handling a Request.”