How can I use $this->renderView inside a symfony Command (not inside a controller)? I new about the function "renderView" but what do I have to setup to use it wihtin a command?
Thank you in advance an regards
If your controller extends from the AbstractController , use the render () helper: If your controller does not extend from AbstractController, you'll need to fetch services in your controller and use the render () method of the twig service. Inject the twig Symfony service into your own services and use its render () method.
The most tipical usage of the templating engine inside a command, is to render the email template dinamically. In this article, we will explain you how to request the templating engine service inside a symfony command or to work with an isolated Twig environment. A. Using the project's environment
When using the Console component in a standalone project, use Application and extend the normal \PHPUnit\Framework\TestCase. Whenever an exception is thrown while running commands, Symfony adds a log message for it including the entire failing command.
Inject the twig Symfony service into your own services and use its render () method. When using service autowiring you only need to add an argument in the service constructor and type-hint it with the Environment class:
Your command class must extends the ContainerAwareCommand
abstract class and then you can do:
$this->getContainer()->get('templating')->render($view, $parameters);
When it comes to commands that extend ContainerAwareCommand
the proper way to obtain the container is by getContainer()
unlike in controller shortcut.
In Symfony 4 I could not get $this->getContainer()->get('templating')->render($view, $parameters);
to work.
I set the namespace use for Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
and extended ContainerAwareCommand class EmailCommand extends ContainerAwareCommand
I get an exception thrown
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "templating".
For Symfony 4, this is the solution I came up with.
First I installed Twig.
composer require twig
Then created my own twig service.
<?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);
}
}
Now my email command looks like this.
<?php
# src/Command/EmailCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Output\OutputInterface,
App\Service\Twig;
class EmailCommand extends Command {
protected static $defaultName = 'mybot:email';
private $mailer,
$twig;
public function __construct(\Swift_Mailer $mailer, Twig $twig) {
$this->mailer = $mailer;
$this->twig = $twig;
parent::__construct();
}
protected function configure() {
$this->setDescription('Email bot.');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$template = $this->twig->load('templates/email.html.twig');
$message = (new \Swift_Message('Hello Email'))
->setFrom('[email protected]')
->setTo('[email protected]')
->setBody(
$template->render(['name' => 'Fabien']),
'text/html'
);
$this->mailer->send($message);
}
}
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