I need to render a twig template from a command class in symfony2.
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('[email protected]') ->setTo('[email protected]') ->setCharset('UTF-8') ->setContentType('text/html') ->setBody($this->renderView('mainBundle:Email:default.html.twig')); $this->getContainer()->get('mailer')->send($message); $output->writeln('Enviado!'); } }
But when I execute the command php app/console send:emails
I get the following error:
Fatal error: Call to undefined method
IT\bBundle\Command\CronCommand::renderView()
How can I render the view?
- Let's start by taking a general look at what Swift Mailer is and what it does. It's a PHP library that you incorporate into your own applications to send email. It's rich in features designed to overcome the shortcomings of the PHP mail function. Swift Mailer was originally created in 2005 by Chris Corbyn.
Symfony's Mailer & Mime components form a powerful system for creating and sending emails - complete with support for multipart messages, Twig integration, CSS inlining, file attachments and a lot more. Get them installed with: $ composer require symfony/mailer.
If you have file system access to the project Look inside the file for a line like: const VERSION = '5.0. 4'; that's the Symfony version number.
It's because renderView is method of class Controller. Instead of that try:
$this->getContainer()->get('templating')->render(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With