gpt4 book ai didi

php - 在 symfony2 中从命令行发送电子邮件

转载 作者:IT王子 更新时间:2023-10-29 01:14:28 25 4
gpt4 key购买 nike

我需要从 symfony2 的命令类中渲染一个 twig 模板。

namespace IT\bBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CronCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('send:emails')
->setDescription('Envio programado de emails');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$message = \Swift_Message::newInstance()
->setSubject('bla bla')
->setFrom('x@x.com')
->setTo('x@gmail.com')
->setCharset('UTF-8')
->setContentType('text/html')
->setBody($this->renderView('mainBundle:Email:default.html.twig'));

$this->getContainer()->get('mailer')->send($message);
$output->writeln('Enviado!');
}
}

但是当我执行命令 php app/console send:emails 我得到以下错误:

Fatal error: Call to undefined method IT\bBundle\Command\CronCommand::renderView()

如何渲染 View ?

最佳答案

因为 renderView 是类 Controller 的方法。而不是尝试:

$this->getContainer()->get('templating')->render(...);

关于php - 在 symfony2 中从命令行发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12408094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com