0

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 ?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
DavidG
  • 149
  • 3
  • 9

1 Answers1

1

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

Akhil Clement
  • 575
  • 1
  • 7
  • 17
  • another option is to change the messenger configuration of `Symfony\Component\Mailer\Messenger\SendEmailMessage` from `async` to `sync` (make sure the `sync transport` isn't commented). This makes it send the emails immediatly rather tham queueing them to be consumed with a cron - that way it still has the HTTP context and the url comes out correctly – 537mfb Jun 03 '23 at 17:02