2

I am encountering precisely this problem: Symfony Docs - How to Generate URLs and Send Emails from the Console

Our email templates are being filled with "localhost" instead of "my.real-domain.name". When constructing links to the application using twig's "url('some/path')".

However, where Symfony is usually "one installation per domain", our application is designed so a single instance can handle multiple domains. It constructs the necessary configuration through various configuration channels, with each customer being one channel.

Thus I would like to avoid configuring "router.request_context.host" and others for every single customer channel.

So I would like to grab the domain to be used from a "--domain" console parameter that we give to every console command instad.

But instead of doing it in every single command, I would need to do this in one central location that grabs the domain and configures "router.request_context" dynamically according to the console parameter.

Is there any way I can do that?

Worp
  • 948
  • 4
  • 17
  • 30

2 Answers2

0

You can register a listener on the event dispatcher which listens to the ConsoleEvents::COMMAND and configure it further before the run method on the command is called.

For example, you can use a setter or change the input on the ConsoleCommandEvent instance.

https://symfony.com/doc/3.4/components/console/events.html

I'm not sure this is an ideal solution, but this is what I could think of at the moment.

Haralan Dobrev
  • 7,617
  • 2
  • 48
  • 66
0

You can tell to the router which domain use when generating an absolute URL, as example:

    $domain = $input->getArgument('domain');
    $context = $this->getContainer()->get('router')->getContext();
    $context->setHost($domain);
    $route = $this->getContainer()->get('router')->generate('welcome_page', $params, UrlGeneratorInterface::ABSOLUTE_URL);

    $output->writeln('Use the following url to show the welcome page for the provided domain');
    $output->writeln(sprintf('<info>%s</info>', $route));

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115