2014-01-12 34 views
1

我在我的控制器下面的代碼:flashMessage對Symfony2的重定向不工作

$this->get('session')->getFlashBag()->add(
        'storeinfo', 
        'hayooo' 
       ); 
    return $this->redirect($this->generateUrl('AppMainBundle_item_detailed_view', array('id' => $picture->getId(), 'caption' => $picture->getURLCaption())), 301); 

和我的樹枝看起來像這樣:

{% if app.session.flashbag.get('storeinfo') %} 
    <div class="comment-confirmation"> 
     {% for flashMessage in app.session.flashbag.get('storeinfo') %} 
      <p> <b> anjing banget </b></p> 
     {% endfor %} 
    </div> 
</div> 
{% else %} 
    <p> Oopsie </p> 
{% endif %} 

如此這般的第一個if塊但是裏面forloop,沒有flashMessage。爲什麼是這樣?

回答

0

的FOSUserBundle確實像this,你也可以看看的FlashListener

{% for type, messages in app.session.flashbag.all() %} 
    {% for message in messages %} 

     {# Will print all your messages #} 
     {{ message }} 

     {# Will do something in particular for storeinfo if you want to #} 
     {% if type is sameas('storeinfo') %} 
      <p> <b> anjing banget </b></p> 
     {% endif %} 

    {% endfor %} 
{% endfor %} 
這個偉大imlementation