我收到以下错误,
[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".
Run Code Online (Sandbox Code Playgroud)
我正在从symfony 2自定义控制台命令渲染twig模板
下面是我的服务类,它是事件订阅者,我通过symfony console命令触发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)
);
.......
}
}
Run Code Online (Sandbox Code Playgroud)
$ body传递给swiftmailer发送电子邮件.
这是我的服务定义,
ACME\MessagingBundle \用户\ NotificationSubscriber
<services>
<service id="notification_subscriber" class="%notification_subscriber.class%">
<argument type="service" id="service_container" />
<tag name="kernel.event_subscriber" />
</service>
</services>
Run Code Online (Sandbox Code Playgroud)
下面的帖子说问题在symfony 2.1中得到修复,但我仍然收到错误,
https://github.com/symfony/symfony/issues/4514
Run Code Online (Sandbox Code Playgroud)
我已经参考了http://symfony.com/doc/current/cookbook/service_container/scopes.html,我已将整个容器传递给我的服务.
vis*_*hal 26
不确定这是否是最好的方式,但添加这对我有用,
$this->container->enterScope('request');
$this->container->set('request', new Request(), 'request');
Run Code Online (Sandbox Code Playgroud)
正如Stof所引述:
如果您使用基于请求的资产助手(从请求对象获取基本URL),则无法从CLI中使用它,因为您没有请求
翻译,asset
如果您打算在CLI中使用该功能,则无法使用模板中的功能.
归档时间: |
|
查看次数: |
7704 次 |
最近记录: |