2015-06-29 24 views
7

$ this-> renderSymfony Komutu içinde nasıl kullanabilirim (bir denetleyicinin içinde değil)? "RenderView" işleviyle ilgili yenilik yapıyorum ancak bir komutla kullanmak için ne yapmalıyım?Symfony Komut kullanımında RenderView Komut kullanımı

$this->getContainer()->get('templating')->render($view, $parameters); 

o kabı elde etmek ContainerAwareCommand uygun şekilde uzatmak komutlara gelince:

cevap

17

hakimiyetin sınıf zorunluluk ContainerAwareCommandabstract class uzanır ve daha sonra yapabileceğiniz bir Saygılarımızla şimdiden teşekkürler denetleyici kısayolunun aksine getContainer()'dir.

+0

Teşekkür ederim, bu nasıl mükemmel çalışıyor. Cevabınız için teşekkürler! – TheTom

+0

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

+0

Belki de $ this-> get ('security.context') -> getToken() === null ' – mykiwi

0

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); 
    } 
} 
+0

Hizmetler, kap derleme işlemi sırasında kaldırılan varsayılan anlamıyla özel olduğundan, bu nedenle 'debug: container komutunu çalıştırarak bunu doğrulayabilirsiniz. "EngineInterface" komutunu enjekte edebilirdiniz ve otomatik olarak Twig ile kablolu hale getirebilirsiniz. –

+0

@Mike temel hizmetlerin, varsayılan olarak konsolda kullanılabilir olmaması için kullanılabilir mi? Düzenleme (sadece sizi gördü çözüm) –

+0

Yani özel servisleri herkese açık kılmak mı istiyorsunuz? c) (https://symfony.com/blog/new-in-symfony-3-4-services-are-private-by-default) 'services.yml'nizde veya benzeri yerlerde. –