本文介绍了Symfony2 资产按文件版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

Symfony2 是否可以使用assets_version by file?

背景

我们正在使用 assets_version 和 assets_version_format管理文件版本并强制更新 CDN 和浏览器缓存.

这很有效!但是,我们发现所有使用的静态资源只有一个 assets_version 参数.

这是一个问题,因为我们的 web 应用程序有大量静态资源,而且我们每天都在向生产环境部署更改.这种情况会杀死缓存.:(

这是我们当前的配置:

config.yml

框架:模板:引擎:['twig']资产版本:%assets_version%assets_version_format: "stv%%2$s/%%1$s";# 资产配置资产:调试:%kernel.debug%使用控制器:假# java:/usr/bin/java过滤器:cssrewrite:~关闭:jar: %kernel.root_dir%/java/compiler.jaryui_css:jar: %kernel.root_dir%/java/yuicompressor-2.4.6.jar

sometemplate.html.twig

 {% 样式表 'bundles/webapp/css/funCommon.css''bundles/webapp/css/funMobile.css'过滤器='?yui_css'%}<link rel=stylesheet href='{{asset_url }}'>{% 结束样式表 %}{% javascripts 'bundles/webapp/js/app.js''bundles/webapp/js/utils.js'过滤器='?关闭'%}<script src="{{asset_url }}"></script>{% endjavascripts %}{% javascripts 'bundles/webapp/js/moduleX.js''bundles/webapp/js/utilsX.js'过滤器='?关闭'%}<script src="{{asset_url }}"></script>{% endjavascripts %}

当我更改任何 css 文件或模块 JS 或任何其他文件时,所有路径都会更改.

我想通过javascript/stylesheet twig标签的参数来管理assets_version_format的版本参数.

这就是我要找的:

{% javascripts 'bundles/webapp/js/app.js''bundles/webapp/js/utils.js'filter='?closure' **version='XX'** %}<script src="{{asset_url }}"></script>{% endjavascripts %}
解决方案

经过多天的搜索,我在 AsseticBundlepackages 选项/p>

http://symfony.com/doc/2.0/reference/configuration/framework.html#full-default-configuration

使用该配置选项,我可以执行以下操作:

{% javascripts 文件 package='packageName' %}

{{asset(file,packageName)}}

示例:

config.yml

框架:模板:引擎:['twig']资产版本:%assets_version%assets_version_format: "stv%%2$s/%%1$s"包:css:版本:6.1version_format: "stv%%2$s/%%1$s"应用程序:版本:4.2version_format: "stv%%2$s/%%1$s"

sometemplate.html.twig

{% javascripts 'bundles/webapp/js/app.js''bundles/webapp/js/utils.js'过滤器='?关闭'包='jsApp'%}<script src="{{asset_url }}"></script>{% endjavascripts %}

输出结果是:

<script src="http://static.domain.com/stv4.2/js/HASH.js"></script>

对我来说,这是按文件管理资产版本的最简单方法.

Question

Is it possible on Symfony2 use assets_version by file?

Background

We are using assets_version and assets_version_format to manage the files version and force the cache update on CDN's and browser cache.

This is working like charm!, but, we found that there is only one assets_version parameters for all the static resources used.

That is a problem since our webapp, has a long amount of static resources and we are deploying changes to prod environment daily. This situation kills the cache. :(

This is our current config:

config.yml

framework:

    templating:
        engines: ['twig']
        assets_version: %assets_version%
        assets_version_format:  "stv%%2$s/%%1$s"

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    # java: /usr/bin/java
    filters:
         cssrewrite: ~
         closure:
             jar: %kernel.root_dir%/java/compiler.jar
         yui_css:
             jar: %kernel.root_dir%/java/yuicompressor-2.4.6.jar

sometemplate.html.twig

    {% stylesheets 'bundles/webapp/css/funCommon.css'
                       'bundles/webapp/css/funMobile.css'
                       filter='?yui_css'
        %}
        <link rel=stylesheet href='{{ asset_url }}'>
        {% endstylesheets %}

    {% javascripts 'bundles/webapp/js/app.js'
                   'bundles/webapp/js/utils.js'
                    filter='?closure'  %}
        <script src="{{ asset_url }}"></script>
        {% endjavascripts %}

{% javascripts 'bundles/webapp/js/moduleX.js'
                   'bundles/webapp/js/utilsX.js'
                    filter='?closure'  %}
        <script src="{{ asset_url }}"></script>
        {% endjavascripts %}

When I change any css file or a module JS or any other file, all paths are changed.

I would like to manage the version parameter of assets_version_format by parameter of javascript/stylesheet twig tag.

This is what I'm looking for:

{% javascripts 'bundles/webapp/js/app.js'
               'bundles/webapp/js/utils.js'
                filter='?closure' **version='XX'** %}
    <script src="{{ asset_url }}"></script>
    {% endjavascripts %}

解决方案

After many days searching I found the packages option on AsseticBundle

http://symfony.com/doc/2.0/reference/configuration/framework.html#full-default-configuration

Using that config option I could do something like this:

{% javascripts file package='packageName' %}

or

{{asset(file,packageName)}}

Sample:

config.yml

framework:

    templating:
        engines: ['twig']
        assets_version: %assets_version%
        assets_version_format:  "stv%%2$s/%%1$s"
        packages:
             css:
                version: 6.1
                version_format: "stv%%2$s/%%1$s"
             jsApp:
                version: 4.2
                version_format: "stv%%2$s/%%1$s"

sometemplate.html.twig

<link rel=stylesheet href='{{ asset('bundles/webapp/css/funCommon.css','css') }}'>

{% javascripts 'bundles/webapp/js/app.js'
               'bundles/webapp/js/utils.js'
                filter='?closure'
                package='jsApp'
 %}
    <script src="{{ asset_url }}"></script>
 {% endjavascripts %}

The output of this is:

<link rel=stylesheet href="http://static.domain.com/stv6.1/css/HASH.css">

<script src="http://static.domain.com/stv4.2/js/HASH.js"></script>

For me that was the simplest way to manage assets version by file.

这篇关于Symfony2 资产按文件版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:48