Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot inject Templating on Symfony 4 Service

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!

like image 386
iamjc015 Avatar asked Mar 18 '18 05:03

iamjc015


2 Answers

You have to install symfony/templating

composer require symfony/templating

change a little bit config/packages/framework.yaml

framework:
    templating:
        engines:
            - twig
like image 103
Adrian Waler Avatar answered Nov 11 '22 22:11

Adrian Waler


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'));
    }
}
like image 24
Macr1408 Avatar answered Nov 11 '22 21:11

Macr1408