[英]Attempted to call an undefined method named “renderView” of class
I'm pretty new in php symfony2. 我在php symfony2中很新。 I want to manage one class for sending emails. 我想管理一门发送电子邮件的课程。
services.yml: services.yml:
mail_helper:
class: HelpBundle\MailSender\EmailManager
arguments: ['@mailer','@templating']
EmailManager: 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". 试图调用类“ HelpBundle \\ MailSender \\ EmailManager”的名为“ renderView”的未定义方法。
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: 您必须在服务中注入模板,以便在services.yml中使用:
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 并将renderView()更改为render(),因为renderView()是控制器的快捷键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.