2012-09-11 17 views

cevap

13

Sadece komutun aynısını yapın. komutunu() işlevini yürütün:

$mailer  = $this->getContainer()->get('mailer'); 
    $transport = $mailer->getTransport(); 

    if ($transport instanceof \Swift_Transport_SpoolTransport) { 
     $spool = $transport->getSpool(); 
     if ($spool instanceof \Swift_ConfigurableSpool) { 
      $spool->setMessageLimit($input->getOption('message-limit')); 
      $spool->setTimeLimit($input->getOption('time-limit')); 
     } 
     if ($spool instanceof \Swift_FileSpool) { 
      if (null !== $input->getOption('recover-timeout')) { 
       $spool->recover($input->getOption('recover-timeout')); 
      } else { 
       $spool->recover(); 
      } 
     } 
     $sent = $spool->flushQueue($this->getContainer()->get('swiftmailer.transport.real')); 

     $output->writeln(sprintf('sent %s emails', $sent)); 
    } 

Sen $ çıkışını kaldırmak gerekir -> ... çizgi (belki $ gönderilen değişkenle yararlı bir şeyler yapabilirsiniz). Ayrıca, bu kod biriktirme iki çeşit için arar, belki de bu türlerden biri değilse, tüm kodu gerekmez.

+0

:) teşekkürler Carlos – Tom

22

Bu, How can I run symfony 2 run command from controller ile de elde edilebilir, bu nedenle kodu çoğaltmazsınız. Benim için çalıştı.

services.yml:

services: 
    swiftmailer.command.spool_send: 
     class: Symfony\Bundle\SwiftmailerBundle\Command\SendEmailCommand 
     calls: 
      - [ setContainer, ["@service_container"] ] 

Kontrolör kodu (basitleştirilmiş): Bir cazibe gibi çalıştı

$this->get('swiftmailer.command.spool_send')->run(new ArgvInput(array()), new ConsoleOutput()); 
+0

Bu kesinlikle daha iyi cevaptır. –

+0

Evet ve bunu yapmanın resmi bir yolu budur - http://symfony.com/doc/3.2/console/command_in_controller.html –

İlgili konular