我有以下课程:
电子邮件通知
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');
}
}
Run Code Online (Sandbox Code Playgroud)
我的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']
Run Code Online (Sandbox Code Playgroud)
但是,当我试图运行我的应用程序时,它抛出一个异常说:
无法自动服务"App\Component\Notification\RealTimeNotification\EmailNotification":方法"__construct()"的参数"$ twigEngine"具有"Symfony\Bundle\FrameworkBundle\Templating\EngineInterface"类型,但未找到此类.
为什么会这样?
谢谢!
小智 28
你必须安装symfony/templating
composer require symfony/templating
Run Code Online (Sandbox Code Playgroud)
改变一点config/packages/framework.yaml
framework:
templating:
engines:
- twig
Run Code Online (Sandbox Code Playgroud)
Mac*_*408 11
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'));
}
}
Run Code Online (Sandbox Code Playgroud)
最有可能您的项目中没有模板,在Symfony 4中,您必须明确要求它:
composer require symfony/templating
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9389 次 |
最近记录: |