我的错误越来越少,
[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被传递给迅捷邮件发送电子邮件。
这是我的服务定义,
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我已经引用了container/scopes.html,我已经把整个容器传递给了我的服务。
发布于 2013-08-06 12:13:09
不知道这是不是最好的方法,但增加这个对我来说是有效的,
$this->container->enterScope('request');
$this->container->set('request', new Request(), 'request');发布于 2013-07-30 11:43:26
正如Stof引用的那样:
如果您使用基于请求的资产帮助器(从请求对象获取基url ),它实际上不能从CLI中使用,因为您在那里没有请求。
如果您打算从CLI中使用asset函数,则不能在模板中使用它。
发布于 2014-06-24 09:28:30
出现这个问题是因为您在模板中使用了资产(),这取决于请求,而且命令行应用程序中没有请求。
快速修复:
framework:
templating:
assets_base_urls: { http: ["http://yoursite.com"], ssl: ["http://yoursite.com"] }https://stackoverflow.com/questions/17942738
复制相似问题