Symfony `url()` Twig 函数在 CLI 中执行时如何确定绝对路径?

How does Symfony `url()` Twig function determine the Absolute Path when executed in CLI?

假设我们的 Twig 模板 包含以下代码:{{ absolute_url(path('__Controller__')) }}{{ url('__Controller__') }}.

当Twig在包含上述功能的Symfony(即:2.7)命令中执行时生成HTML,如何确定绝对路径?

它从 RequestContext 读取它。在 HTTP 请求期间从请求数据自动填充,但需要在 CLI 环境中明确设置。

您可以手动配置它:

$context = $this->container->get('router')->getContext();
$context->setHost('mydomain.com');
$context->setScheme('https');

或者让 Symfony 通过设置以下参数自动为您完成:

# app/config/parameters.yml
parameters:
    router.request_context.host: mydomain.com
    router.request_context.scheme: https

Symfony 文档有一篇文章对此进行了解释:https://symfony.com/doc/2.8/console/request_context.html