Symfony 4'te çalışmak için $this->getContainer()->get('templating')->render($view, $parameters);
alamadım.
Ben
Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
için ad kullanımını ayarlayabilir ve ben Symfony 4 için
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "templating".
atılmış bir istisna olsun
ContainerAwareCommand class EmailCommand extends ContainerAwareCommand
genişletilmiş, bu şimdiye ile geldi çözümdür.
Önce Twig'i kurdum.
composer require twig
Daha sonra kendi twig hizmetimi oluşturdum.
<?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);
}
}
Şimdi e-posta komutum şunun gibi görünüyor.
<?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);
}
}
Teşekkür ederim, bu nasıl mükemmel çalışıyor. Cevabınız için teşekkürler! – TheTom
Komutla ilgili başka bir soru: Komutada security.content'e nasıl erişebilirim? $ user = $ this-> get ('security.context') -> getToken() -> getUser(); bu işe yaramayacak, bu yüzden tekrar sıkıştım :( – TheTom
Belki de $ this-> get ('security.context') -> getToken() === null ' – mykiwi