无法在 Symfony 4 服务上注入模板
Cannot inject Templating on Symfony 4 Service
我有以下 class:
电子邮件通知
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']
但是,当我尝试 运行 我的应用程序时,它抛出一个异常说:
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.
为什么会这样?
谢谢!
很可能您的项目中没有包含模板,在 Symfony 4 中您必须明确要求它:
composer require symfony/templating
你必须安装symfony/templating
composer require symfony/templating
稍微改一下config/packages/framework.yaml
framework:
templating:
engines:
- twig
我设法用 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'));
}
}
我有以下 class:
电子邮件通知
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']
但是,当我尝试 运行 我的应用程序时,它抛出一个异常说:
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.
为什么会这样?
谢谢!
很可能您的项目中没有包含模板,在 Symfony 4 中您必须明确要求它:
composer require symfony/templating
你必须安装symfony/templating
composer require symfony/templating
稍微改一下config/packages/framework.yaml
framework:
templating:
engines:
- twig
我设法用 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'));
}
}