Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash Message disappears on redirect in Symfony 2.1

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?

like image 386
Fonsini Avatar asked Sep 26 '12 14:09

Fonsini


2 Answers

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 %}
like image 188
Elnur Abdurrakhimov Avatar answered Oct 21 '22 21:10

Elnur Abdurrakhimov


I figured it out.

Flash messages were not appearing due to session issues.

Symfony 2.1 now uses session.storage.native for storage_id and handler_id by default.

Please check how this session issue was solved here.

like image 45
Fonsini Avatar answered Oct 21 '22 20:10

Fonsini