Symfony2/Twig:生成用于 CDN 的备用绝对 URL 路径?
这个 stackoverflow 解决方案 几乎可以回答我的问题.但我想在 Twig
中生成 CDN url,而不仅仅是资源.我想为动态内容生成它们.所以我不认为 Assetic 调整是合适的领域.
This stackoverflow solution almost answers my question. But I want to generate CDN urls in Twig
to more than just resources. I'd like to generate them for dynamic content. So I don't think an Assetic tweak is the right area to be looking.
现在,我可以在 parameters.ini
中设置 CDN_url 并在我的 url 中使用它.我的代码如下所示: {{CDN_Url}}{{url('route',{'param1':'value'}}
.. 代码维护是我不喜欢的主要原因之一这个选项.然后你可以做一些事情,比如检查 /cdn/
的路由以生成 CDN url 和其他一切都基于域.我不必到处更改很多变量.如果我想阻止 CDN url 生成.我可以修改路由.所以弄清楚是否有一个干净的解决方案有很多好处.
For now, I can set CDN_url in parameters.ini
and use that in my urls. My code would look like this : {{CDN_Url}}{{url('route',{'param1':'value'}}
.. Code maintenance is one major reason I don't like this option. Then you could do things like check the route for /cdn/
to generate the CDN url and everything else to be based on the domain. I wouldn't have to run around changing a lot of variables. AND if I wanted to stop the CDN url from generating. I could just modify the route. So there's a lot of benefits to figuring out if there's a clean solution to this.
如果还没有解决方案 - 我将如何开始扩展 {{ url() }}
功能,以便我可以像 path
和 <代码>网址.
If, there's not a solution already - how would I start to extend the {{ url() }}
functionality so that I could use it like path
and url
.
推荐答案
您可以通过以下方式在您链接的问题的帮助下完成
You can do it with the help of the question you linked by following way
{{ asset(path('route',{'param1':'value'})) }}
如果您需要处理多个CDN域名,您可以通过以下方式完成
If you need to handle multiple CDN domains you can do it by following way
在 app/config.yml
# app/config.yml
#....
templating:
engines: ['twig']
packages:
cdn1:
base_urls: ["http://cdn1.domain.com"]
cdn2:
base_urls: ["http://cdn2.domain.com"]
然后在你的树枝模板文件中
And then in your twig template file
{{ asset('path/of/file', 'cdn1')
或
{{ asset('path/of/file', 'cdn2')
相关文章