Symfony2 Twig Asset Base_Url is being Unpredictable?

php, symfony, twig

Solution

You can switch to the `url` twig function, it's the same as `path` but this one will always return an absolute url:

url('cropImageSm', {'filename': mobj.getFilename })

Further Reference:

`Asset()` will use the host supplied in the config.yml

framework:
    templating:
        packages:
            {althostname}:
                base_urls:  { http: ["http://www.domain2.com"], ssl: "/"] }

So `{{ asset(path('route1', {'page': 1 }),'althostname') }}` would output.. `http://www.domain2.com/route1/page/1`

Problem

I'm using a Twig loop to generate `<img src="">` for multiple images. My eventual use of this code is to take advantage of a CDN with base_url in the Framework configuration. Below is the `Twig` code : ``` {% for key, mobj in productmedia.getImages %} <img src="{{ asset(path('cropImage', {'filename': mobj.getFilename }),'cdn') }}" /> {% endfor %} {% for key, mobj in productmedia.getImages %} <img src="{{ asset(path('cropImageSm', {'filename': mobj.getFilename }),'cdn') }}" /> {% endfor %} ``` I would expect, at the very least for there to be some consistency in the output.. but it seems SF2 will sometimes use one base_url and sometimes not use one. Why would this be? Below is the output. ``` <img src="http://cdn.cloudfront.net/cri/1/matt-skydiving.SH340_SW340.jpg" /> <img src="/cri/1/Swimming.SH340_SW340.jpg" /> <img src="http://cdn.cloudfront.net/cri/1/successman2.SH340_SW340.jpg" /> <img src="/cri/1/matt-skydiving.SH40_SW40.jpg" /> <img src="http://cdn.cloudfront.net/cri/1/Swimming.SH40_SW40.jpg" /> <img src="http://cdn.cloudfront.net/cri/1/successman2.SH40_SW40.jpg" /> ``` As you can see in the first loop.. some inherit the base_url.. and some don't.. I've cleared all cache.. done all the general debugging steps.. It doesn't make sense why this isn't consistent.

Original source