सिम्फनी 4 में मुझे $this->getContainer()->get('templating')->render($view, $parameters);
काम करने के लिए नहीं मिला।
मैं Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
के लिए नाम स्थान उपयोग की स्थापना की और ContainerAwareCommand class EmailCommand extends ContainerAwareCommand
बढ़ाया मैं एक अपवाद फेंका
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "templating".
Symfony 4 के लिए मिलता है, यह समाधान मैं के साथ आया है।
सबसे पहले मैंने ट्विग स्थापित किया।
composer require twig
फिर अपनी खुद की जुड़वां सेवा बनाई।
<?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);
}
}
अब मेरा ईमेल कमांड इस तरह दिखता है।
<?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);
}
}
स्रोत
2018-02-19 19:25:34
धन्यवाद, यह पूरी तरह से काम करता है। खैर, आपके उत्तर के लिए धन्यवाद !! – TheTom
कमांड के बारे में सिर्फ एक और प्रश्न: मैं कमांड में सुरक्षा.content कैसे एक्सेस कर सकता हूं? $ उपयोगकर्ता = $ यह-> प्राप्त करें ('security.context') -> getToken() -> getUser(); यह काम नहीं करेगा इसलिए मैं फिर से अटक गया :( – TheTom
शायद क्योंकि '$ this-> प्राप्त करें (' security.context ') -> getToken() === null' – mykiwi