73

I have an application with Symfony2 (2.2). When I want to send a mail, I'm having trouble with the paths, which are all relative paths and obviously aren't working inside emails

for rendering 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 therefore I need to append the domain. I can do something like:

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

but this is not the best solution for my problem, as I have different domains.

I found the solution for paths with the url function but I still need a solution for assets.

2

10 Answers 10

138

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
4
  • 4
    This seems to be the most up-to-date answer so far.
    – BHarms
    May 16, 2016 at 21:52
  • Symfony 2.3.* has an url helper
    – piotr_cz
    Jun 26, 2016 at 12:15
  • 3
    This is the best answer for V3.0 +
    – Miles M.
    Dec 27, 2016 at 20:09
  • 1
    Hi @Axel, that is expected. The CLI doesn't know what your domain name is. However, you can configure your request context, here is how symfony.com/doc/2.8/console/request_context.html Nov 23, 2018 at 7:22
85

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  #}
2
  • 4
    you can shortcut this to {{ app.request.getSchemeAndHttpHost ~ asset('images/foo.jpg') }} ah didnt see your update :D
    – john Smith
    May 12, 2014 at 9:33
  • 4
    This answer seems to be somewhat outdated. See the answer from Neto form Sep 24 '15.
    – BHarms
    May 16, 2016 at 21:46
53

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!" />
1
  • yeah is for new developments :|
    – rkmax
    Aug 8, 2014 at 16:58
27

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

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

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

You have to 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 path function:

{{ absolute_url(path('index')) }}
1
  • 1
    FYI absolute_url works on 4.3, this should be accepted answer
    – Mirgen
    Jul 23, 2020 at 19:34
13

You probably want to use the assets_base_urls configuration.

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

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


Note that the configuration is different since Symfony 2.7:

framework:
    # ...
    assets:
        base_urls:
            - 'http://cdn.example.com/'
2
  • I updated the URL and noticed that the code changed too. I took the liberty to update your answer. Feel free to revert the changes.
    – A.L
    Mar 18, 2016 at 13:14
  • @A.L thanks for the edit, I wasn't aware of the change. Mar 18, 2016 at 14:56
4

It's possible to have http://test.example and https://production.example. Then hard coding the URL is a bad idea. I would suggest this:

{{app.request.scheme ~ '://' ~ app.request.host ~ asset('bundle/myname/img/image.gif')}}
2
  • You can override configs for test and dev environments (config.yml, config_dev.yml, ...), so that's not an issue.
    – dxvargas
    Apr 28, 2014 at 13:46
  • 1
    always renders http for me - even in https
    – tishma
    Oct 6, 2014 at 15:11
4

This is working at least in newer Symfony projects:

{{ absolute_url(asset('bundle/myname/img/image.gif')) }}
3

The following works for me:

<img src="{{ asset('bundle/myname/img/image.gif', null, true) }}" />
2

Additional info to generate absolute URL using a command (to send an email for instance)

In a command, {{ absolute_url(path('index')) }} is not working out of the box.

You will need to add the additional configuration shown in antongorodezkiy's answer.

But in case you don't want to change the configuration because you are not sure how it could impact the whole app, you can configure the router in the command.

Here is the doc :

https://symfony.com/doc/3.4/console/request_context.html

Here is the code :

use Symfony\Component\Routing\RouterInterface;
// ...

class DemoCommand extends Command
{
    private $router;

    public function __construct(RouterInterface $router)
    {
        parent::__construct();

        $this->router = $router;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $context = $this->router->getContext();
        $context->setHost('example.com');
        $context->setScheme('https');
        $context->setBaseUrl('my/path');

        $url = $this->router->generate('route-name', ['param-name' => 'param-value']);
        // ...
    }
}

To generate the URL in the Twig template

<a href="{{ absolute_url(path(...)) }}"></a>

You can fetch the HOST and SCHEME from your env file

$context = $this->router->getContext();
$context->setHost($_ENV['NL_HOST']);
$context->setScheme($_ENV['NL_SCHEME']);

Just define the variable in .env and .env.local files

NL_HOST=mydomain.com
NL_SCHEME=https
1

I've used the following advice from the docs https://symfony.com/doc/current/console/request_context.html to get absolute urls in emails:

# config/services.yaml
parameters:
    router.request_context.host: 'example.org'
    router.request_context.scheme: 'https'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.