gpt4 book ai didi

Symfony 2.7+ Assets 自定义版本策略配置

转载 作者:行者123 更新时间:2023-12-02 13:46:31 25 4
gpt4 key购买 nike

我已成功设置 Symfony 2.7.x 安装以使用标准 Assets 版本策略(EmptyVersionStrategy 和 StaticVersionStrategy),但想要实现 Custom Version Strategy例如基于日期的策略或类似策略。

目前,我有

config.yml

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

由于 version 值似乎实现了策略以及值,因此如何配置自定义策略?

我已经阅读了 Asset Blog articleless-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+ Assets 自定义版本策略配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32927560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com