I'm currently writing a newsletter tool, and therefore have to generate absolute URLs in a CLI script which is called via cron.
Unfortunately the Symfony CLI command does not know anything about my host/base_url, so the router generates absolute URLs with a wrong base_url. It always uses http://localhost
as base.
Is there a way to tell the router the correct base_url?
My code:
$this->container->get('router')->generate($route, $parameters, true);
$host = $this->container->getParameter('host'); $this->container->get('router')->getContext()->setHost($host);
You can do it in this way:
Similarly you can set baseurl and scheme:
Since 2.1 you can configure the default parameters of the router, which is probably the best approach. A CLI script will use these default parameters, however a web request will override them:
For more details, see How to Generate URLs and Send Emails from the Console