我需要在 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
}
}
关于php - Symfony2 : Retrieve app. 来自命令的 Twig 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26237081/