Symfony Templating内部服务

时间:2015-09-23 15:09:45

标签: symfony service twig templating

我已经定义了一个看起来像这样的服务:

namespace PrBundle\Service;

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

class PrMailService extends \Twig_Extension{

   public function __construct(\Swift_Mailer $mailer,\Twig_Environment $twig )
{
    $this->twig = $twig;
    $this->mailer = $mailer;
}

public function sendMail($messageSubject, $from, $to,$array)
{

    $message = \Swift_Message::newInstance()
        ->setSubject($messageSubject)
        ->setFrom($from)
        ->setTo($to)
        ->setBody(
            $this->twig->render('PrBundle:Email:basic_email_template.html.twig',
                array(
                    $array
                )
            ),
            'text/html'
        );

    $this->mailer->send($message);

}

在config.yml中,我将服务设置如下:

services:    
  PrMailService:
      class: PrBundle\Service\PrMailService
      arguments: ["@mailer", "@templating"]

至少,我无法呈现模板app / console test:service,它会死于以下错误消息:

[Symfony\Component\Debug\Exception\ContextErrorException]Catchable Fatal Error: 
Argument 2 passed to PrBundle\Service\PrMailService::__construct() must be an instance of Twig_Environment, instance of Symfony\Bundle\TwigBundle\Debug\TimedTwigEngine given.

在app / console容器中:debug,有"模板"定义为TimedTwigEngine。这让我有点困惑。任何人都可以给我一个提示吗?

0 个答案:

没有答案