如何使用Assetic与CacheBustingWorker和twig

ivo*_*oba 2 symfony twig assetic

我可以看到Assetic在CacheBusting上取得了一些进展:https:
//github.com/kriswallsmith/assetic#cache-busting

但我真的不明白我应该如何使用它.
这可以在树枝内使用:

{% stylesheets 'bundles/mybundle/css/fonts.css' 
               'bundles/mybundle/css/style.css'
               'bundles/mybundle/css/screen.css'
               filter='cssrewrite'
 %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
Run Code Online (Sandbox Code Playgroud)

用通常的assetic:dump命令?

我必须在哪里挂钩CacheBustingWorker?

Jue*_*gen 23

Cache buster现在是symfony/AsseticBundle(Version> = 2.5.0)的一部分.

在composer.json中更改AsseticBundle版本,如下所示:

"symfony/assetic-bundle": "2.5.0",
Run Code Online (Sandbox Code Playgroud)

并激活config.yml文件中的资产缓存清除

assetic:
    workers:
        cache_busting: ~
Run Code Online (Sandbox Code Playgroud)

我的JS文件现在看起来像这样:

web/bundles/js/projectname-876f9ee.js
Run Code Online (Sandbox Code Playgroud)

请参阅https://github.com/symfony/AsseticBundle/pull/119#issuecomment-28877145


Dim*_*dis 5

我最近一直在寻找如何做同样的事情.

我想出的解决方案是用我自己的类覆盖Symfony的AssetFactory,并在其构造函数中添加CacheBustingWorker.基本上你创建一个如下文件:

<?php
namespace YourSite\YourBundle\Factory;

use Symfony\Bundle\AsseticBundle\Factory\AssetFactory as BaseAssetFactory;
use Assetic\Factory\Worker\CacheBustingWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class AssetFactory extends BaseAssetFactory
{
    public function __construct(KernelInterface $kernel, ContainerInterface $container, ParameterBagInterface $parameterBag, $baseDir, $debug = false)
    {
        parent::__construct($kernel, $container, $parameterBag, $baseDir, $debug);
        // Add CacheBustingWorker
        $this->addWorker(new CacheBustingWorker(CacheBustingWorker::STRATEGY_CONTENT));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后更改assetic.asset_factory.class参数以指向配置中的这个新类.在我的情况下,我将以下内容添加到config.yml:

parameters:
  assetic.asset_factory.class: YourSite\YourBundle\Factory\AssetFactory
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我还发现有一个PR待定,可以整合到AsseticBundle中:https://github.com/symfony/AsseticBundle/pull/119 (2认同)