简体   繁体   English

Symfony2 / Twig:生成用于CDN的备用绝对URL路径?

[英]Symfony2 / Twig : Generate Alternate Absolute URL Path for use with CDN?

This stackoverflow solution almost answers my question. 这个stackoverflow解决方案几乎回答了我的问题。 But I want to generate CDN urls in Twig to more than just resources. 但我想在Twig生成CDN网址,而不仅仅是资源。 I'd like to generate them for dynamic content. 我想为动态内容生成它们。 So I don't think an Assetic tweak is the right area to be looking. 因此,我不认为Assetic调整是正确的领域。

For now, I can set CDN_url in parameters.ini and use that in my urls. 现在,我可以在parameters.ini设置CDN_url并在我的网址中使用它。 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. 我的代码看起来像这样: {{CDN_Url}}{{url('route',{'param1':'value'}} ..代码维护是我不喜欢这个选项的一个主要原因。然后你可以做比如检查/cdn/的路由来生成CDN网址和其他所有基于域的内容。我不需要改变很多变量。如果我想阻止CDN网址生成。我可以修改路线。所以要弄清楚是否有一个干净的解决方案有很多好处。

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 . 如果,还没有解决方案 - 我将如何开始扩展{{ url() }}功能,以便我可以像pathurl一样使用它。

You can do it with the help of the question you linked by following way 您可以通过以下方式在您链接的问题的帮助下完成此操作

{{ asset(path('route',{'param1':'value'})) }}

If you need to handle multiple CDN domains you can do it by following way 如果您需要处理多个CDN域,可以按照以下方式执行

In app/config.yml 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 然后在你的twig模板文件中

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

OR 要么

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

I don't know about the CDN stuff but as far as extending the url function, take a look at: 我不知道CDN的东西,但就扩展url功能,请看看:

Symfony\\Bridge\\Twig\\Extension\\RoutingExtension Symfony的\\桥\\嫩枝\\延期\\ RoutingExtension

public function __construct(UrlGeneratorInterface $generator)
{
    $this->generator = $generator;
}
public function getFunctions()
{
    return array(
        'url'  => new \Twig_Function_Method($this, 'getUrl'),
        'path' => new \Twig_Function_Method($this, 'getPath'),
    );
}
public function getPath($name, $parameters = array())
{
    return $this->generator->generate($name, $parameters, false);
}

public function getUrl($name, $parameters = array())
{
    return $this->generator->generate($name, $parameters, true);
}

So you could get at getUrl there or you could insert you own UrlGenerator. 所以你可以在那里获得getUrl,或者你可以插入自己的UrlGenerator。 Not sure which would be easier in your specific case. 不确定在特定情况下哪个更容易。 Probably making your own UrlGenerator. 可能会制作自己的UrlGenerator。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM