我正遇到这个问题:Symfony Docs - How to Generate URLs and Send Emails from the Console
我们的电子邮件模板正在填充“localhost”而不是“my.real-domain.name”。使用twig的“url('some / path')”构建应用程序的链接时。
但是,在Symfony通常“每个域一个安装”的情况下,我们的应用程序被设计为单个实例可以处理多个域。它通过各种配置渠道构建必要的配置,每个客户都是一个渠道。
因此,我想避免为每个客户渠道配置“router.request_context.host”和其他人。
所以我想从我们给每个控制台命令instad的“--domain”控制台参数中获取要使用的域。
但是我不需要在每个命令中执行此操作,而是需要在一个中心位置执行此操作,该位置抓取域并根据控制台参数动态配置“router.request_context”。
有什么方法可以做到吗?
您可以在事件调度程序上注册侦听器,该侦听器侦听ConsoleEvents::COMMAND
并在调用命令的run
方法之前进一步配置它。
例如,您可以使用setter或更改ConsoleCommandEvent
实例上的输入。
https://symfony.com/doc/3.4/components/console/events.html
我不确定这是一个理想的解决方案,但这是我现在能想到的。
您可以告诉路由器在生成绝对URL时使用哪个域,例如:
$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));
希望这有帮助