Symfony生成CDN友好资产网址 [英] Symfony generate cdn friendly asset url

查看:81
本文介绍了Symfony生成CDN友好资产网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由CDN(例如akamai)提供服务的symfony应用程序,并且我在模板上生成资产网址:

I have a symfony application that gets served by a cdn (eg. akamai) and I generate the asset url on the templates with:

{{ asset('myJs.js') }}

我想要的时候生成类似(假设该网站由 www.example.com 服务)的url:

When I do I want to generate a url like (supposing that the site is served by www.example.com) :


www.example.com/myJs.js?v=321321312312

www.example.com/myJs.js?v=321321312312

其中的v参数在为了强制CDN(例如akamai网络)缓存我较新的版本。动态生成 v 参数的好方法吗?

Where the v parameter get a deployed version in order to force cdn (eg. akamai network) to cache me newer version. Is it a good way to dynamically generate the v parameter?

推荐答案

如果要将散列追加到每个版本保持唯一的文件,则可以使用该文件的文件修改时间或散列。最好的方法可能是创建一个新的Twig函数versionedAsset(),该函数使用asset()的输出并将此哈希附加到生成的URL上

If you want to append a hash to the file that is kept unique per version, you could either use the file modification time of that file or a hash. The best way could be to create a new Twig function versionedAsset() that uses the output of asset() and appends this hash to the generated URL

这篇关于Symfony生成CDN友好资产网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆