Absolute URL in template returns localhost in email templates

1.4k Views Asked by At

I'm using absolute_url function defined here in my twig email template that is triggered via symfony command but the path only returns localhost/route instead of the complete URL http://abc.local/route.

<a href="{{ absolute_url('downloads') }}">download</a>

What am I missing here ?

1

There are 1 best solutions below

1
Akhil Clement On

Solution with Symfony-5.4

Generating URLs in commands works the same as generating URLs in services. The only difference is that commands are not executed in the HTTP context. Therefore, if you generate absolute URLs, you'll get http://localhost/ as the host name instead of your real host name.

The solution is to configure the default_uri option to define the "request context" used by commands when they generate URLs: On config/packages/routing.yaml add the URL of the real host.

# config/packages/routing.yaml    
framework:
    router:
        # ...
        default_uri: 'https://example.org/my/path/'

The default_uri option was introduced in Symfony 5.1.

For reference please see the official documentation. https://symfony.com/doc/5.4/routing.html#generating-urls-in-commands