How to use absolute path in branch functions

I have an application with Symfony2 (2.2). When I want to send mail, I have problems with paths, which are relative paths and obviously do not work inside emails.

to display the paths I'm using:

<a href="{{ path('route_name', {'param' : value}) }}">A link</a> 

and for assets:

 <img src="{{ asset('bundle/myname/img/image.gif') }}" alt="Title"/> 

The previous examples work fine, but the paths are relative, so I need to add a domain. I can do something like:

 <a href="http://domain.com{{ path('route_name', {'param' => param1}) }}">A link</a> 

but this is not the best solution for my problem, since I have different areas.

Update

I found a solution for the paths using the url function, but I still need an asset solution.

+43
symfony twig routing
Jun 11 '13 at 16:58
source share
7 answers

For Symfony 2.7 and later

See this one here.

1st working option

 {{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset('bundles/acmedemo/images/search.png') }} 

2nd working option - preferred

Just did a quick test with a clean new copy of Symfony. There is also another option that combines the scheme and httpHost:

 {{ app.request.getSchemeAndHttpHost() ~ asset('bundles/acmedemo/images/search.png') }} {# outputs #} {# http://localhost/Symfony/web/bundles/acmedemo/css/demo.css #} 
+63
Jun 11 '13 at 17:19
source share

Symfony 2.7 has a new absolute_url that can be used to generate an absolute URL. http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component#template-function-changes

It will work in both cases or in the path line:

 <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"/> 

in these tree cases you will get an absolute url like

 http://www.example.com/my/absolute/path 
+77
Sep 24 '15 at 20:02
source share

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 absolute argument) to true:

Example:

 <img src="{{ asset('images/logo.png', absolute=true) }}" alt="Symfony!" /> 
+49
Aug 08 '14 at 16:43
source share

Daniel's answer seems to work fine, but note that generating absolute URLs using the twig asset function is now deprecated:

DEPRECATED - Creating absolute URLs using the Twig () asset function was deprecated in 2.7 and will be removed in 3.0. Please use absolute_url ().

Here's the official announcement: http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component#template-function-changes

You should use the absolute_url twig function:

 {# Symfony 2.6 #} {{ asset('logo.png', absolute = true) }} {# Symfony 2.7 #} {{ absolute_url(asset('logo.png')) }} 

It is interesting to note that it also works with the path function:

 {{ absolute_url(path('index')) }} 
+20
Jul 20 '15 at 18:53
source share

You might want to use the assets_base_urls configuration.

 framework: templating: assets_base_urls: http: [http://www.website.com] ssl: [https://www.website.com] 

http://symfony.com/doc/current/reference/configuration/framework.html#assets




Note that the configuration is different from Symfony 2.7 :

 framework: # ... assets: base_urls: - 'http://cdn.example.com/' 
+11
Jun 11 '13 at 18:24
source share

Maybe http://test_site.com and https://production_site.com . Then hardcoding url is a bad idea. I would suggest the following:

 {{app.request.scheme ~ '://' ~ app.request.host ~ asset('bundle/myname/img/image.gif')}} 
+3
Mar 26 '14 at 10:05
source share

The following works for me:

 <img src="{{ asset('bundle/myname/img/image.gif', null, true) }}" /> 
+3
Jul 24 '14 at 14:19
source share



All Articles