简体   繁体   English

尝试调用类的名为“ renderView”的未定义方法

[英]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.

相关问题 试图调用一个未定义的名为“查询”的类的方法 - Attempted to call an undefined method named “query” of class 尝试调用类的名为“ getEntityManager”的未定义方法 - Attempted to call an undefined method named “getEntityManager” of class 尝试调用类“ App \\ Twig \\ AppExtension”中名为“ getDoctrine”的未定义方法 - Attempted to call an undefined method named “getDoctrine” of class “App\Twig\AppExtension” 尝试调用类“ Swift_Mailer”的名为“ getContext”的未定义方法 - Attempted to call an undefined method named “getContext” of class “Swift_Mailer” 尝试调用类“ AppBundle \\ Controller \\ DefaultController”的未定义方法“ getRequest” - Attempted to call an undefined method named “getRequest” of class “AppBundle\Controller\DefaultController” 试图调用 class “App\Controller\ApiController” 的名为“getPosition”的未定义方法 - Attempted to call an undefined method named “getPosition” of class “App\Controller\ApiController” PHP 7.2.7:尝试调用类“ZipArchive”的名为“setEncryptionName”的未定义方法 - PHP 7.2.7: Attempted to call an undefined method named “setEncryptionName” of class “ZipArchive” 尝试调用类“ Zend \\ Http \\ Client”中名为“ request”的未定义方法 - Attempted to call an undefined method named “request” of class “Zend\Http\Client” 尝试调用类“ DOMElement”的未定义方法“ filter” - Attempted to call an undefined method named “filter” of class “DOMElement” 尝试调用类“ Knp \\ Menu \\ MenuItem”的名为“ setCurrentUri”的未定义方法 - Attempted to call an undefined method named “setCurrentUri” of class “Knp\Menu\MenuItem”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM