首页 文章

无法在Symfony 4服务上注入模板

提问于
浏览
3

我有以下课程:

EmailNotification

namespace App\Component\Notification\RealTimeNotification;

use Symfony\Bridge\Twig\TwigEngine;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

use App\Component\Notification\NotificationInterface;

class EmailNotification implements NotificationInterface
{   
    private $logNotification;

    public function __construct(LogNotification $logNotification, \Swift_Mailer $mailer,  EngineInterface $twigEngine)
    {
        $this->logNotification = $logNotification;
    }

    public function send(array $options): void
    {
        $this->logNotification->send($options);

        dump('Sent to email');
    }
}

我的yml上有以下服务定义:

app.email_notification:
    class: App\Component\Notification\RealTimeNotification\EmailNotification
    decorates: app.log_notification
    decoration_inner_name: app.log_notification.inner
    arguments: ['@app.log_notification.inner', '@mailer', '@templating']

但是,当我试图运行我的应用程序时,它抛出一个异常说:

无法自动服务“App \ Component \ Notification \ RealTimeNotification \ EmailNotification”:方法“__construct()”的参数“$ twigEngine”具有“Symfony \ Bundle \ FrameworkBundle \ Templating \ EngineInterface”类型,但未找到此类 .

为什么会这样?

谢谢!

2 回答

  • 2

    很可能你的项目中没有包含模板,在Symfony 4中你必须明确要求它:

    composer require symfony/templating
    
  • 11

    你必须安装symfony / templating

    composer require symfony/templating
    

    改变一点config / packages / framework.yaml

    framework:
        templating:
            engines:
                - twig
    

相关问题