How to use absolute path in twig functions
From Symfony2 documentation: Absolute URLs for assets were introduced in Symfony 2.5.
If you need absolute URLs for assets, you can set the third argument (or the absolute argument) to true:
Example:
<img src="{{ asset('images/logo.png', absolute=true) }}" alt="Symfony!" />
For Symfony 2.7 and newer
See this answer here.
1st working option
{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset('bundles/acmedemo/images/search.png') }}
2nd working option - preferred
Just made a quick test with a clean new Symfony copy. There is also another option which combines scheme and httpHost:
{{ app.request.getSchemeAndHttpHost() ~ asset('bundles/acmedemo/images/search.png') }}
{# outputs #}
{# http://localhost/Symfony/web/bundles/acmedemo/css/demo.css #}
Symfony 2.7 has a new absolute_url which can be used to generate the absolute url. http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component#template-function-changes
It will work on those both cases or a path string:
<a href="{{ absolute_url(path('route_name', {'param' : value})) }}">A link</a>
and for assets:
<img src="{{ absolute_url(asset('bundle/myname/img/image.gif')) }}" alt="Title"/>
Or for any string path
<img src="{{ absolute_url('my/absolute/path') }}" alt="Title"/>
on those tree cases you will end up with an absolute URL like
http://www.example.com/my/absolute/path