3

我已成功设置我的 Symfony 2.7.x 安装以使用标准资产版本策略(EmptyVersionStrategy 和 StaticVersionStrategy),但我想实现自定义版本策略,如基于日期的策略或类似策略。

目前,我有

配置.yml

framework:
    assets:
        version: 'v1' # or ~ for EmptyVersionStrategy

由于version值似乎实现了策略和值,我该如何配置自定义策略?

我已经阅读了资产博客文章不完整的文档

4

1 回答 1

3

您可以EmptyVersionStrategy轻松替换,只需覆盖服务,如下所示:

应用程序/配置/服务.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));
    }
}
于 2016-05-23T14:23:29.373 回答