【问题标题】:Cannot inject Templating on Symfony 4 Service无法在 Symfony 4 服务上注入模板
【发布时间】:2018-08-26 22:30:48
【问题描述】:

我有以下课程:

电子邮件通知

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']

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

无法自动绑定服务 “应用\组件\通知\RealTimeNotification\EmailNotification”: 方法“__construct()”的参数“$twigEngine”具有类型 "Symfony\Bundle\FrameworkBundle\Templating\EngineInterface" 但是这个 找不到类。

为什么会这样?

谢谢!

【问题讨论】:

    标签: symfony symfony4


    【解决方案1】:

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

    composer require symfony/templating
    

    【讨论】:

    • 我确实有,我检查了三倍。
    • 我现在注入的是\Twig_Environment,我认为现在就足够了。
    • @iamjc015 当你运行php bin/console debug:autowiring 时,你看到Symfony\Component\Templating\EngineInterface 类了吗?它应该是templating.engine.twig 的别名。你的 framework.yaml 文件中有 templating 条目吗?
    • 我没有看到引擎接口。也许这就是问题所在,为什么会这样?
    • 我没有模板。是的,没有 symfony/模板 :(
    【解决方案2】:

    你必须安装 symfony/模板

    composer require symfony/templating
    

    稍微修改一下 config/packages/framework.yaml

    framework:
        templating:
            engines:
                - twig
    

    【讨论】:

    • 对于那些想知道的人,您必须在framework.yaml 中添加配置部分,以便自动装配以加载模板组件。
    • 还有更多信息要添加到这里吗?我收到一个错误 You have requested a non-existent service "templating.engine.twig". ... I have installed composer 需要 symfony/template`?
    • 我不确定这是否有帮助,但我所做的是:composer require twig,之后我就可以使用'@templating'
    • symfony/templating 自 Symfony 4.3 起已被弃用。请参阅stackoverflow.com/a/58380813/1668200 以获得解决方案。
    【解决方案3】:

    我设法用 Twig 环境和 HTTP 响应做到了

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

    【讨论】:

    • 这是自 Symfony 4.3 以来的首选方式,因为“模板”组件已被弃用。少一个依赖!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多