简体   繁体   English

如何在Symfony 4服务中使用renderView和twig模板

[英]How to use renderView and twig templating in a Symfony 4 Service

I'm creating a Emailer Service in my new Symfony 4 application. 我正在新的Symfony 4应用程序中创建一个Emailer服务。

I have tried a million things but no luck. 我尝试了一百万件但没有运气。 I could only find a few resources on this topic for S4 at the moment. 我目前只能为S4找到关于这个主题的一些资源。 Any help is appreciated. 任何帮助表示赞赏。

This what I'm trying to achieve. 这就是我想要实现的目标。 I understand I have to use different services inside of my Emailer service but no luck. 我知道我必须在我的Emailer服务中使用不同的服务,但没有运气。

<?php

namespace App\Mailer;

class Emailer
{

    public function sendWelcome($email): \Swift_Mailer
    {
        $message = (new \Swift_Message('P****** - Welcome In!'))
            ->setFrom('no-reply@p****n.com')
            ->setTo($email)
            ->setBody(
                $this->renderView(
                // templates/emails/registration.html.twig
                    'emails/registration.html.twig',
                    array('name' => $user->getUsername())
                ),
                'text/html'
            )
            ->setCharset('utf-8');

        $mailer->send($message);

        return true;
    }
}

First you need to get your templating service injected into your class (constructor injection) and then you can use it to render template. 首先,您需要将您的模板服务注入您的类(构造函数注入),然后您可以使用它来呈现模板。

In the code you can see it that we type-hint it in constructor so Symfony Dependency injection know what we need. 在代码中,您可以看到我们在构造函数中对其进行类型提示,因此Symfony Dependency注入知道我们需要什么。 Then we just use it. 然后我们就使用它。 Same will be with your $mailer service. 您的$mailer服务也是如此。

<?php

namespace App\Mailer;

use Symfony\Component\Templating\EngineInterface;

class Emailer
{

    /**
     * @var EngineInterface
     */
    private $templating;

    /**
     * TestTwig constructor.
     */
    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

    public function sendWelcome($email): \Swift_Mailer
    {
        $message = (new \Swift_Message('P****** - Welcome In!'))
            ->setFrom('no-reply@p****n.com')
            ->setTo($email)
            ->setBody(
                $this->templating->render(
                // templates/emails/registration.html.twig
                    'emails/registration.html.twig',
                    array('name' => $user->getUsername())
                ),
                'text/html'
            )
            ->setCharset('utf-8');

        $mailer->send($message);

        return true;
    }
}

@miles-m a use statement is not the same as injection. @ miles -m use语句与注入不同。 A use statement just makes the class accessible with the class name as an alias. use语句只是use类名作为别名来访问类。 Dependency Injection is a pattern that decouples your classes from each other which facilitates better testing and debugging (you can swap out your injected objects for mock objects etc). 依赖注入是一种将类彼此分离的模式,这有助于更好地进行测试和调试(您可以将注入的对象替换为模拟对象等)。

One way to inject the Swift_Mailer would be as a constructor parameter, ie 注入Swift_Mailer的一种方法是作为构造函数参数,即

class Emailer
{
    /** @var \Swift_Mailer $mailer */
    private $mailer;    

    public function __construct(
        EngineInterface $templating,
        \Swift_Mailer $mailer <== mailer will be injected here
    ) : \Swift_Mailer
    {
        //...
        $this->mailer->send($message);
    }
}

class CallingClass
{
    //...
    $emailer = new Emailer(
        //EngineInterface instance
        //\Swift_Mailer instance <== injecting 
    );
    $emailer->sendWelcome('email@example.com');
}

Other questions 其他问题

  1. $mailer->send($message)

    Where is your $mailer instance defined? 您的$ mailer实例在哪里定义?

  2. public function sendWelcome($email): \\Swift_Mailer

    return true;

    Is true a valid instance of Swift_Mailer ? trueSwift_Mailer的有效实例吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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