我目前正在编写新闻通讯工具,因此必须在通过 cron 调用的 CLI 脚本中生成绝对 URL。
不幸的是,Symfony CLI 命令对我的 host/base_url 一无所知,因此路由器生成带有错误 base_url 的绝对 URL。它始终使用 http://localhost
作为基础。
有没有办法告诉路由器正确的 base_url?
我的代码:
$this->container->get('router')->generate($route, $parameters, true);
最佳答案
你可以这样做:
$host = $this->getContainer()->getParameter('host');
$this->getContainer()->get('router')->getContext()->setHost($host);
同样可以设置baseurl和scheme:
$this->getContainer()->get('router')->getContext()->setScheme('https');
$this->getContainer()->get('router')->getContext()->setBaseUrl('/web');
关于php - Symfony2 : how to set the host/base url in CLI scripts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503250/