2
votes

I have a problem with Symfony 2.

I get the CSS in the head section like this:

<?php foreach ($view['assetic']->stylesheets(
    array(
        '../web/assets/css/bootstrap.min.css',
        '../web/assets/css/main.css',
    ),array('cssrewrite')
) as $url): ?>
    <link rel="stylesheet" href="<?php echo $view->escape($url) ?>" />
<?php endforeach ?>

In the dev environment, everything works. In the prod environment, the CSS files are generated but the HTML rendering fails with an exception before printing the <link> tag.

In my log I see this, but I don't understand the error.

[Sun Jul 19 21:21:55.615090 2015] [:error] [pid ] [client X:61902] PHP Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Catchable Fatal Error: Argument 1 passed to Symfony\Bundle\AsseticBundle\Templating\StaticAsseticHelper::__construct() must be an instance of Symfony\Component\Templating\Helper\CoreAssetsHelper, instance of Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper given, called in /home/web/public_html/web/app/cache/prod/appProdProjectContainer.php on line 296 and defined' in /home/web/public_html/web/vendor/symfony/assetic-bundle/Templating/StaticAsseticHelper.php:33\nStack trace:\n#0 /home/web/public_html/web/vendor/symfony/assetic-bundle/Templating/StaticAsseticHelper.php(33): Symfony\Component\Debug\ErrorHandler->handleError(4096, 'Argument 1 pass...', '/home/web/....', 33, Array)\n#1 /home/web/public_html/web/app/cache/prod/appProdProjectContainer.php(296): Symfony\Bundle\AsseticBundle\Templating\StaticAsseticHelper->__construct(Object(Symfony\Bundle\FrameworkBundle\Templating\ in /home/web/public_html/web/vendor/twig/twig/lib/Twig/Parser.php on line 370

I've tried clearing the cache, but that doesn't change anything.

2
Have you done app/console cache:clear --env=prod?user2268997
Yes, I tried but the same problem happends.Carlos Vázquez

2 Answers

1
votes

I encountered the same problem on Symfony 2.7 so I assume this is the version you are using.

The problem is that the Assetic bundle creates a helper to serve assets. This helper requires a Symfony\Component\Templating\Helper\CoreAssetsHelper instance in its constructor: https://github.com/symfony/AsseticBundle/blob/master/Templating/StaticAsseticHelper.php#L33

So far, this was working fine as this was the class which was registered for the service that is injected into the constructor. However, with Symfony 2.7 this service was refactored. The upgrade notes for 2.7 read:

The templating.helper.assets was refactored and returns now an object of the type Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper instead of Symfony\Component\Templating\Helper\CoreAssetsHelper. You can update your class definition or use the assets.packages service instead. Using the assets.packages service is the recommended way. The templating.helper.assets service will be removed in Symfony 3.0.

While the new class provides the same methods as the deprecated one, the constructor of the StaticAsseticHelper class has a type hint for the deprecated class. This leads to the error message you are seeing.

IMHO this is a backwards compatibility break for Symfony 2.7 and I have filed a bug report with the Symfony team. In the mean time, however, this has been fixed in AsseticBundle and upgrading this to the newest version should resolve the problem.

Workaround

If you can, for some reason, not upgrade AsseticBundle, here is how to temporarily fix it.

As mentioned above, the problem lies with the type hint on the StaticAsseticHelper constructor. So we will change this to use the new type and we are good to go.

Locate the file symfony/assetic-bundle/Templating/StaticAsseticHelper.php in your vendor directory.

Change line 17 from

use Symfony\Component\Templating\Helper\CoreAssetsHelper;

to

use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;

and change line 33 from

public function __construct(CoreAssetsHelper $assetsHelper, AssetFactory $factory)

to

public function __construct(AssetsHelper $assetsHelper, AssetFactory $factory)

and the problem will be fixed for now. However, this fix will be overwritten the next time you update your Assetic bundle via composer.

0
votes

Did you try to change the assets paths? Read this documentation for details. You need write paths like bundles/AppBundle/....