Php Symfony细枝:获取内核根目录
我已在Symfony项目上设置了以下配置:Php Symfony细枝:获取内核根目录,php,twig,symfony-3.3,Php,Twig,Symfony 3.3,我已在Symfony项目上设置了以下配置: twig: debug: '%kernel.debug%' strict_variables: '%kernel.debug%' globals: web_dir: "%kernel.root_dir%/../web" 我已经完成了以下细枝符号函数(如中所示): 我的问题是如何将web_dirglobal的值放入versionedAsset函数中 编辑1 我使用Symfony的autowire,并自动连接/自动配置
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
globals:
web_dir: "%kernel.root_dir%/../web"
我已经完成了以下细枝符号函数(如中所示):
我的问题是如何将web_dir
global的值放入versionedAsset函数中
编辑1
我使用Symfony的autowire,并自动连接/自动配置allextensions
类:
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
// To use as default template
$definition = new Definition();
$definition
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false);
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository,Resources,Tests}');
您可以通过将扩展声明为服务,然后将服务容器传递给它来实现这一点:
twig.all.extensions:
class: AppBundle\Twig\AllExtentions
arguments:
- @service_container
tags:
- { name: twig.extension }
在此之后,将一个\u construct()
方法添加到扩展中,并使用它获取一个yourweb\u 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;
}
您可以通过将扩展声明为服务,然后将服务容器传递给它来实现这一点:
twig.all.extensions:
class: AppBundle\Twig\AllExtentions
arguments:
- @service_container
tags:
- { name: twig.extension }
在此之后,将一个\u construct()
方法添加到扩展中,并使用它获取一个yourweb\u 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;
}
发布此扩展的服务定义I使用Symfony的autowire。发布此扩展的服务定义I使用Symfony的autowire。如何使用php注释而不是yml?如何使用php注释而不是yml?