您可以通过将扩展声明为
服务
然后经过一个
服务容器
致:
twig.all.extensions:
class: AppBundle\Twig\AllExtentions
arguments:
- @service_container
tags:
- { name: twig.extension }
在此之后添加
__construct()
方法,并使用它获取您的
web_dir
变量:
/**
* ContainerInterface $container
*/
public function __construct($container)
{
$this->container = $container;
}
/**
* Gebnerate a cdn friendly url for the assets.
* @param string $path The url of the path RELATIVE to the css.
* @return string
*/
public function versionedWebAsset($path)
{
$webDir=$this->container->get('twig')->getGlobals()['web_dir'];
$hash=hash_file("sha512",$path);
return $path."?v=".$hash;
}