php - Symfony2 : Retrieve app. 来自命令的 Twig 请求

标签 php symfony twig

我需要在 Symfony2 命令中生成邮件模板,一切正常,除了 {{app.request}} 在 Twig 中为空(我需要它用于 sheme 和 httpHost),因为它是从 cli 上下文调用的。我试图通过以下方式更改该范围:

$this->getContainer()->enterScope('request');
$this->getContainer()->set('request', new Request(), 'request');

但它不提供 app.request 。是否有解决此问题的解决方案?

最佳答案

Symfony 指南建议全局配置请求上下文,这样您就可以对参数进行静态配置,并以编程方式设置 Symfony 路由器组件的上下文。

# app/config/parameters.yml
parameters:
  router.request_context.host: example.org
  router.request_context.scheme: https
  router.request_context.base_url: my/path


 // src/Acme/DemoBundle/Command/DemoCommand.php

 // ...
class DemoCommand extends ContainerAwareCommand
{
 protected function execute(InputInterface $input, OutputInterface $output)
 {
    $context = $this->getContainer()->get('router')->getContext();
    $context->setHost('example.com');
    $context->setScheme('https');
    $context->setBaseUrl('my/path');

    // ... your code here
 }
}

There is a specific paragraph for this problem on the guide

关于php - Symfony2 : Retrieve app. 来自命令的 Twig 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26237081/

相关文章:

php - 我无法将 1.5MB 以上的图像上传到服务器上的目录

php - Mysql 到 PDO 代码转换

php - 刷新 oauth2 token google api 和 HWIOAuthBundle

css - 如何使用 Grav 显示部分导航

Symfony2 呈现表单小部件 form_row()(HTML5 考虑)

php - 如何使用 PHP 从 URL 获取基础域名?

php - 两个不同 div 中没有一定长度的文本

symfony - Symfony2 日志中的那些空方括号是什么?

symfony - 设置 Nelmio ApiDoc 返回参数说明

php - 在我的自定义类中注入(inject) Silex $app