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
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
