Flash Message disappears on redirect in Symfony 2.1

Fonsini picture Fonsini · Sep 26, 2012 · Viewed 8.7k times · Source

I'm migrating from Symfony 2.0 to Symfony 2.1.

I have the following simple code on my controller:

public function createEntidadeAction() {
    $this->get('session')->getFlashBag()->set('error', 'message');
    return $this->redirect($this->generateUrl('EntidadeBundle_index'));
}

If I generate an error (for example by passing a bad route), I check on the profiler that the flash message is there.

However if i let the redirect to succeed, the flash message disappears and nothing is displayed. I have the folloing on my corresponding Twig template:

{% for flashMessage in app.session.flashbag.get('error') %}
    <div class="flash-notice">
        {{ flashMessage }}
    </div>
{% endfor %}

I can not figure this out. What am I missing? Flash messages should last after the first redirect, no?

Answer

Elnur Abdurrakhimov picture Elnur Abdurrakhimov · Sep 26, 2012

First, try using the add method instead of set on the flash bag. Second, try this template which works for me:

{% for type, flashMessages in app.session.flashbag.all() %}
    {% for flashMessage in flashMessages %}
        <div class="alert alert-{{ type }}">
            {{ flashMessage|trans }}
        </div>
    {% endfor %}
{% endfor %}