Symfony2 assets versioning by file

前端 未结 4 835
长情又很酷
长情又很酷 2020-12-31 14:59

Question

Is it possible on Symfony2 use assets_version by file?

Background

We are using assets_version and assets_version_format to

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 15:24

    The following solution will append a timestamp instead of a version number. The important part is that he timestamp will only change when you clear your cache.

    First create a compiler pass:

    removeDefinition('assets._version__default');
            $decorator = new DefinitionDecorator('assets.static_version_strategy');
            $decorator->replaceArgument(0, time());
            $container->setDefinition('assets._version__default', $decorator);
        }
    }
    

    Then add it to your main bundle:

    namespace App\Bundle;
    
    use App\Bundle\DependencyInjection\Compiler\AssetCompilerPass;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    
    class AppBundle extends Bundle
    {
        public function build(ContainerBuilder $container)
        {
            parent::build($container);
    
            $container->addCompilerPass(new AssetCompilerPass());
        }
    }
    

    This will only work on the default asset package. You would need to loop through the different package definitions you have set up if you are using that feature.

    You would need to replace assets._version_ instead of assets._version__default

提交回复
热议问题