RenderView in My service

前端 未结 3 1393
别跟我提以往
别跟我提以往 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:41

    This works on Symfony +4.2, assuming your application's namespace is App and your mailer class service is named EmailService.

    On your Service class:

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

    On your services.yaml

    services:
      email_service:
        class: App\Service\EmailService
        arguments: ['@swiftmailer.mailer.default', '@twig']
    

提交回复
热议问题