Symfony命令使用中的RenderView
如何在symfony命令(而不是控制器)中使用$this->renderView?我对函数“renderView”有了新的了解,但是在命令中使用它需要设置什么呢Symfony命令使用中的RenderView,symfony,render,Symfony,Render,如何在symfony命令(而不是控制器)中使用$this->renderView?我对函数“renderView”有了新的了解,但是在命令中使用它需要设置什么呢 提前感谢您的命令类必须扩展ContainerWareCommand,然后您可以执行以下操作: $this->getContainer()->get('templating')->render($view, $parameters); 当涉及到扩展ContainerWareCommand的命令时,获得容器的正确方法是通
提前感谢您的命令类必须扩展
ContainerWareCommand
,然后您可以执行以下操作:
$this->getContainer()->get('templating')->render($view, $parameters);
当涉及到扩展
ContainerWareCommand
的命令时,获得容器的正确方法是通过getContainer()
而不是控制器快捷方式。在Symfony 4中,我无法获得$this->getContainer()->get('templating')->render($view,$parameters)代码>开始工作
我为Symfony\Bundle\FrameworkBundle\Command\ContainerWareCommand
和扩展的ContainerWareCommandclass EmailCommand扩展了ContainerWareCommand
我得到一个异常抛出
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "templating".
对于Symfony 4,这就是我提出的解决方案
首先我安装了细枝
composer require twig
然后创建了我自己的细枝服务
<?php
# src/Service/Twig.php
namespace App\Service;
use Symfony\Component\HttpKernel\KernelInterface;
class Twig extends \Twig_Environment {
public function __construct(KernelInterface $kernel) {
$loader = new \Twig_Loader_Filesystem($kernel->getProjectDir());
parent::__construct($loader);
}
}
还有一个:依赖依赖依赖注入,即注入容器接口
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Container\ContainerInterface;
class SampleCommand extends Command
{
public function __construct(ContainerInterface $container)
{
$this->templating = $container->get('templating');
parent::__construct();
}
protected function configure()
{
$this->setName('app:my-command')
->setDescription('Do my command using render');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = retrieveSomeData();
$csv = $this->templating->render('path/to/sample.csv.twig',
array('data' => $data));
$output->write($csv);
}
private $templating;
}
这依赖于Symfony注入容器,容器反过来用于检索模板
或细枝
或自定义命令所需的任何内容。谢谢,这就是它的完美工作方式。好的,谢谢你的回复!!关于该命令的另一个问题是:如何访问命令中的security.content$user=$this->get('security.context')->getToken()->getUser();这不起作用,所以我又卡住了:(可能是因为$this->get('security.context')->getToken()==null
Hm-oky,这可能是因为我通过控制台调用它。因此没有人必须登录。uhh我的错。很抱歉,给你提示了thx!!我试过了,但我得到的错误是“你请求了一个不存在的服务”templating“因为服务在默认情况下是私有的,这意味着在容器编译过程中被删除,因此以后不可用。您可以通过运行debug:container templating
命令来验证这一点。您可以改为注入EngineInterface
,这样您将得到自动连接的细枝。@Mike有办法让core se服务,默认情况下控制台不可用,可用?编辑(刚刚看到您的解决方案)你的意思是公开私有服务?当然,就在你的服务.yml
或诸如此类的地方。@Mike谢谢你的时间。我很难将模板声明为公开。我应该在服务中声明为公开的模板名称是什么。yml?你试过EngineInterface吗?如果没有,请将其声明为模板服务的别名但命令作为服务是一种更好的方法。该类更容易测试。自Symfony 4.2以来,使用模板化服务已被弃用
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Container\ContainerInterface;
class SampleCommand extends Command
{
public function __construct(ContainerInterface $container)
{
$this->templating = $container->get('templating');
parent::__construct();
}
protected function configure()
{
$this->setName('app:my-command')
->setDescription('Do my command using render');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = retrieveSomeData();
$csv = $this->templating->render('path/to/sample.csv.twig',
array('data' => $data));
$output->write($csv);
}
private $templating;
}