Symfony2: how to set the host/base url in CLI scripts
You can do it in this way:
$host = $this->getContainer()->getParameter('host');$this->getContainer()->get('router')->getContext()->setHost($host);
Similarly you can set baseurl and scheme:
$this->getContainer()->get('router')->getContext()->setScheme('https');$this->getContainer()->get('router')->getContext()->setBaseUrl('/web');
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:
# app/config/parameters.ymlparameters: router.request_context.host: example.org router.request_context.scheme: https router.request_context.base_url: my/path
For more details, see How to Generate URLs and Send Emails from the Console
$host = $this->container->getParameter('host');$this->container->get('router')->getContext()->setHost($host);