gpt4 book ai didi

symfony - 错误 : You cannot create a service ("templating.helper.assets") of an inactive scope ("request")

转载 作者:行者123 更新时间:2023-12-04 08:59:40 24 4
gpt4 key购买 nike

我得到以下错误,

  [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 自定义控制台命令渲染 Twig 模板

下面是我的服务类,它是事件订阅者,我通过 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 ,我已将整个容器传递给我的服务。

最佳答案

不确定这是否是最好的方法,但添加它对我有用,

    $this->container->enterScope('request');
$this->container->set('request', new Request(), 'request');

关于symfony - 错误 : You cannot create a service ("templating.helper.assets") of an inactive scope ("request"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17942738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com