代码之家  ›  专栏  ›  技术社区  ›  Ricky Notaro-Garcia

在CLI中执行Symfony'url()'Twig函数时,它是如何确定绝对路径的?

  •  0
  • Ricky Notaro-Garcia  · 技术社区  · 8 年前

    让我们假装 细枝模板 包含以下代码: {{ absolute_url(path('__Controller__')) }} 或 {{ url('__Controller__') }} .

    当Twig在 Symfony公司 (即:2.7)包含上述功能的命令,如何确定绝对路径?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Igor Pantović    8 年前

    它从 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 docs有一篇文章对此进行了解释: https://symfony.com/doc/2.8/console/request_context.html

    推荐文章