Symfony2: how to set the host/base url in CLI scripts(Symfony2:如何在 CLI 脚本中设置主机/基本 URL)
问题描述
我目前正在编写新闻通讯工具,因此必须在通过 cron 调用的 CLI 脚本中生成绝对 URL.
I'm currently writing a newsletter tool, and therefore have to generate absolute URLs in a CLI script which is called via cron.
不幸的是,Symfony CLI 命令对我的主机/base_url 一无所知,因此路由器生成带有错误 base_url 的绝对 URL.它总是使用 http://localhost
作为基础.
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.
有没有办法告诉路由器正确的base_url?
Is there a way to tell the router the correct base_url?
我的代码:
$this->container->get('router')->generate($route, $parameters, true);
推荐答案
你可以这样:
$host = $this->getContainer()->getParameter('host');
$this->getContainer()->get('router')->getContext()->setHost($host);
同样可以设置baseurl和scheme:
Similarly you can set baseurl and scheme:
$this->getContainer()->get('router')->getContext()->setScheme('https');
$this->getContainer()->get('router')->getContext()->setBaseUrl('/web');
这篇关于Symfony2:如何在 CLI 脚本中设置主机/基本 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony2:如何在 CLI 脚本中设置主机/基本 URL


基础教程推荐
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- Web 服务器如何处理请求? 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- php中的foreach复选框POST 2021-01-01
- php中的PDF导出 2022-01-01