RenderView in My service

前端 未结 3 1357
别跟我提以往
别跟我提以往 2021-02-05 02:53

I\'m new of symfony world. I want to use render inside my service, but I got this error

Call to undefined method renderView

I know t

3条回答
  •  余生分开走
    2021-02-05 03:40

    Using constructor dependency injection (tested with Symfony 3.4):

    class MyService
    {
        private $mailer;
        private $templating;
    
        public function __construct(\Swift_Mailer $mailer, \Twig_Environment $templating)
        {
            $this->mailer     = $mailer;
            $this->templating = $templating;
        }
    
        public function sendEmail()
        {
            $message = $this->templating->render('emails/registration.html.twig');
    
            // ...
        }
    }
    

    No need to configure arguments.

提交回复
热议问题