15

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!

iamjc015
  • 2,127
  • 6
  • 25
  • 61

3 Answers3

30

You have to install symfony/templating

composer require symfony/templating

change a little bit config/packages/framework.yaml

framework:
    templating:
        engines:
            - twig
Adrian Waler
  • 331
  • 1
  • 3
  • 4
  • 4
    For those wondering, you have to add the config part in `framework.yaml` for autowiring to load the Templating component. – Link14 Aug 22 '18 at 19:29
  • Any more info to add to this? I get an error `You have requested a non-existent service "templating.engine.twig". ... I have installed `composer require symfony/templating`? – BugHunterUK Jan 14 '19 at 15:07
  • I am not sure if that is what helped, but I what is did is: `composer require twig` and after that I was able to use `'@templating'` – aga Jan 25 '19 at 13:56
  • `symfony/templating` is deprecated since Symfony 4.3. See https://stackoverflow.com/a/58380813/1668200 for a solution. – Thomas Landauer Oct 14 '19 at 16:40
24

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'));
    }
}
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
Macr1408
  • 785
  • 7
  • 9
  • 4
    This is the preferred way since Symfony 4.3 as the "templating" component has been deprecated. One dependency less! – Mike Doe Aug 26 '19 at 08:24
11

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

composer require symfony/templating
MakG
  • 1,254
  • 9
  • 14
  • 3
    I actually have, I tripled check it. – iamjc015 Mar 18 '18 at 10:24
  • 4
    What I inject now is \Twig_Environment, i think this would suffice for now. – iamjc015 Mar 18 '18 at 10:25
  • 2
    @iamjc015 When you run `php bin/console debug:autowiring` do you see `Symfony\Component\Templating\EngineInterface` class? It should be an alias to `templating.engine.twig`. Do you have `templating` entry in your framework.yaml file? – MakG Mar 18 '18 at 11:08
  • i don't see EngineInterface. Maybe thats the problem, why is that so? – iamjc015 Mar 19 '18 at 12:26
  • and i don't have templating. Yeah, no symfony/templating :( – iamjc015 Mar 19 '18 at 12:27
  • @iamjc015 I've just used your suggestion, works great. Thanks! There is a typo in your comment though, for Sf4 I had to use \Twig\Environment – Muc Mar 26 '19 at 18:59
  • `symfony/templating` is deprecated since Symfony 4.3. See https://stackoverflow.com/a/58380813/1668200 for a solution. – Thomas Landauer Oct 14 '19 at 16:40