生成供CDN使用的备用绝对URL路径

生成供CDN使用的备用绝对URL路径

本文介绍了Symfony2 / Twig:生成供CDN使用的备用绝对URL路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此几乎可以回答我的问题。但是我想在 Twig 中生成CDN URL,而不仅仅是资源。我想为动态内容生成它们。因此,我不认为应该进行资产调整。

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 并在我的网址中使用它。我的代码看起来像这样: {{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 url 这样使用它。

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')

OR

{{ asset('path/of/file', 'cdn2')

这篇关于Symfony2 / Twig:生成供CDN使用的备用绝对URL路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:14