14
votes

I have the following class:

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');
    }
}

I have the following service definition on my 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']

However, when i tried to run my app it throws an Exception saying:

Cannot autowire service "App\Component\Notification\RealTimeNotification\EmailNotification": argument "$twigEngine" of method "__construct()" has type "Symfony\Bundle\FrameworkBundle\Templating\EngineInterface" but this class was not found.

Why is that so?

Thanks!

3

3 Answers

11
votes

Most likely you don't have Templating included in your project, in Symfony 4 you have to require it explicitly:

composer require symfony/templating
30
votes

You have to install symfony/templating

composer require symfony/templating

change a little bit config/packages/framework.yaml

framework:
    templating:
        engines:
            - twig
23
votes

I managed to do it with Twig Environment and HTTP Response

<?php

namespace App\Controller;

use Twig\Environment;
use Symfony\Component\HttpFoundation\Response;

class MyClass
{
    private $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    public function renderTemplateAction($msg)
    {
        return new Response($this->twig->render('myTemplate.html.twig'));
    }
}