我得到以下错误,
[Twig_Error_Runtime]
An exception has been thrown during the rendering of a template ("You cannot create a service ("templating.helper.assets") of an inactive scope ("request").") in "AcmeMessagingBundle:Comment:email.html.twig".
我正在从 symfony 2 自定义控制台命令渲染树枝模板
下面是我的服务类,它是事件订阅者,我通过 symfony 控制台命令触发 onCommentAddEmail 事件来发送电子邮件,
class NotificationSubscriber implements EventSubscriberInterface
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public static function getSubscribedEvents()
{
return array(
'comment.add' => array('onCommentAddEmail', 0),
);
}
public function onCommentAddEmail(CommentAddEvent $event)
{
...................
$body = $this->container->get('templating')->render(
'AcmeMessagingBundle:Comment:email.html.twig',
array('template' => $template)
);
.......
}
}
$body 被传递给 swiftmailer 以发送电子邮件。
这是我的服务定义,
Acme\MessagingBundle\Subscriber\NotificationSubscriber
<services>
<service id="notification_subscriber" class="%notification_subscriber.class%">
<argument type="service" id="service_container" />
<tag name="kernel.event_subscriber" />
</service>
</services>
下面的帖子说问题已在 symfony 2.1 中修复,但我仍然收到错误,
https://github.com/symfony/symfony/issues/4514
我已经参考了http://symfony.com/doc/current/cookbook/service_container/scopes.html,我已经将整个容器传递给我的服务。