Symfony 2.7+ 资产自定义版本策略配置
Symfony 2.7+ Asset Custom Version Strategy config
我已成功设置我的 Symfony 2.7.x 安装以使用标准资产版本策略(EmptyVersionStrategy 和 StaticVersionStrategy),但想实施 Custom Version Strategy 类似基于日期的策略或类似策略.
目前,我有
config.yml
framework:
assets:
version: 'v1' # or ~ for EmptyVersionStrategy
由于version
值似乎实现了策略和值,我如何配置自定义策略?
我已经阅读了 Asset Blog article and the less-than-complete docs。
您可以轻松替换 EmptyVersionStrategy
,只需覆盖服务,如下所示:
app/config/services.yml
services:
assets.empty_version_strategy:
class: FooBundle\Twig\Extension\AssetVersionStrategy
arguments: ["%kernel.root_dir%"]
src/FooBundle\Twig\AssetVersionStrategy.php
namespace FooBundle\Twig\Extension;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
/**
* AssetVersionStrategy (Twig)
*
* @author Your Name <you@example.org>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class AssetVersionStrategy implements VersionStrategyInterface
{
/**
* @var string
*/
private $root;
/**
* @param string $root
*/
public function __construct($root)
{
$this->root = $root;
}
/**
* Should return the version by the given path (starts from public root)
*
* @param string $path
* @return string
*/
public function getVersion($path)
{
return $path;
}
/**
* Determines how to apply version into the given path
*
* @param string $path
* @return string
*/
public function applyVersion($path)
{
return sprintf('%s?v=%s', $path, $this->getVersion($path));
}
}
我已成功设置我的 Symfony 2.7.x 安装以使用标准资产版本策略(EmptyVersionStrategy 和 StaticVersionStrategy),但想实施 Custom Version Strategy 类似基于日期的策略或类似策略.
目前,我有
config.yml
framework:
assets:
version: 'v1' # or ~ for EmptyVersionStrategy
由于version
值似乎实现了策略和值,我如何配置自定义策略?
我已经阅读了 Asset Blog article and the less-than-complete docs。
您可以轻松替换 EmptyVersionStrategy
,只需覆盖服务,如下所示:
app/config/services.yml
services:
assets.empty_version_strategy:
class: FooBundle\Twig\Extension\AssetVersionStrategy
arguments: ["%kernel.root_dir%"]
src/FooBundle\Twig\AssetVersionStrategy.php
namespace FooBundle\Twig\Extension;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
/**
* AssetVersionStrategy (Twig)
*
* @author Your Name <you@example.org>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class AssetVersionStrategy implements VersionStrategyInterface
{
/**
* @var string
*/
private $root;
/**
* @param string $root
*/
public function __construct($root)
{
$this->root = $root;
}
/**
* Should return the version by the given path (starts from public root)
*
* @param string $path
* @return string
*/
public function getVersion($path)
{
return $path;
}
/**
* Determines how to apply version into the given path
*
* @param string $path
* @return string
*/
public function applyVersion($path)
{
return sprintf('%s?v=%s', $path, $this->getVersion($path));
}
}