Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderView in Symfony Command usage

Tags:

render

symfony

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

like image 308
TheTom Avatar asked Jun 29 '15 11:06

TheTom


People also ask

How do I render a controller in Symfony using twig?

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.

How to use the templating engine inside a Symfony command?

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

How do I use Symfony console in a standalone project?

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.

How to use service autowiring in Symfony?

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:


2 Answers

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.

like image 159
mykiwi Avatar answered Sep 21 '22 10:09

mykiwi


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);
    }
}
like image 20
Francisco Luz Avatar answered Sep 18 '22 10:09

Francisco Luz