我在Stackoverflow中阅读了很多关于这个主题的帖子,但我仍然没有成功。
我正在使用Symfony version 2.2.3
,目前我正在尝试在我的项目中使用flashbag messegaes,但出于某些原因我在Twig模板中收到一个空数组。
这是我的控制器代码:
public function addProductAction($id) {
$session = $this->container->get('session');
// *** Some nive stuff here with session parameters **
$session->set('products', $products);
// Here it is my flashbag code sort like in symfony2 documentation
$session->getFlashBag()->add('notice', 'Your changes were saved!');
$session->save();
return $this->redirect($this->getRequest()->headers->get('referer'));
}
这是我的Twig代码:
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-notice">
{{ flashMessage }}
</div>
{%else%}
<div class="flash-notice">
NO MESSAGES
</div>
{% endfor %}
我这样做就像人们在网上发帖一样,但没有运气......任何想法?因为我认为我只做了一个请求,不明白为什么它不起作用......
答案 0 :(得分:1)
在此之前可能还有另一个get('notice')
。 get
方法(来自FlashBag
类)以这种方式工作,它会删除条目并将其返回(因此它更像是“pop”而不是“get”)。如果您只想获取一个条目而不使用peek
方法从闪存包中删除它。
关于您的问题 - 请确保您之前没有致电get('notice')
。