试图调用名为" renderView"的未定义方法。班级

时间:2017-05-22 11:36:00

标签: php symfony swiftmailer

我在php symfony2中很新。我想管理一个班级来发送电子邮件。

services.yml:

   mail_helper:
      class: HelpBundle\MailSender\EmailManager
      arguments: ['@mailer','@templating']

EmailManager:

class EmailManager
{
protected $mailer;
protected $templating;

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

public function sendEmail($fromEmail,$toEmail,$subject,$comments,$status)
{
    $message = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($fromEmail)
        ->setTo($toEmail)
        ->setBody($this->templating->renderView('HelpBundle:Emails:add-comment.html.twig', array('comments' => $comments,
            'status' => $status,
            'subject' => $subject)))
    ;
    $this->mailer->send($message);
   }
 }

在我的控制器中,我这样称呼:

public newAction()
{
$mailer = $this->get('mail_helper');
    $mailer->sendEmail($fromEmail,$toEmail,$subject,$comments,$ticketStatus);
}

下一次调用控制器操作时出错:

  

尝试调用名为" renderView"的未定义方法。类" HelpBundle \ MailSender \ EmailManager"。

我想了解我如何解决这个问题?

非常感谢

1 个答案:

答案 0 :(得分:2)

你必须在服务中注入一个模板,所以在services.yml中使用:

arguments: ["@mailer", "@templating"]

然后在您的服务中使用它:

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

public function sendEmail($fromEmail, $toEmail, $subject, $comments, $status)
{
    $message = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($fromEmail)
        ->setTo($toEmail)
        ->setBody($this->templating->render('
                 HelpBundle:Emails:add-comment.html.twig', 
                 array('comments' => $comments,
                       'status' => $status,
                       'subject' => $subject
                 )
             )
        );
    $this->mailer->send($message);
}

并将renderView()更改为render(),因为renderView()是控制器快捷方式