试图调用名为 "renderView" 的未定义方法 class

Attempted to call an undefined method named "renderView" of class

我是 php symfony2 的新手。我想管理一个 class 用于发送电子邮件。

services.yml:

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

电子邮件管理员:

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

下一个调用控制器动作时出错:

Attempted to call an undefined method named "renderView" of class "HelpBundle\MailSender\EmailManager".

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

非常感谢

您必须在您的服务中注入一个模板,因此在 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() 是控制器快捷方式