简体   繁体   中英

Attempted to call an undefined method named “renderView” of class

I'm pretty new in php symfony2. I want to manage one class for sending emails.

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

In my controller I have called like this:

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

Error when controller action is called is next:

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

I want to understand how I can fix this ?

Thanks a lot

You have to inject into your service a templating so in services.yml use:

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

and then use it in your service:

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

And change renderView() to render(), because renderView() is controller shortcut

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM