5

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?

Fonsini
  • 693
  • 8
  • 18

2 Answers2

7

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 %}
Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
  • The problem is not the way i am displaying it. The flash message literally disapears. If i look at the profiler, the flash message is not there. Right after the first redirect. Using add did not change this. Maybe this is some php configuration. I do have this in the config.yml: `session: storage_id: session.storage.mock_file` so it can have the php.ini configs. – Fonsini Sep 26 '12 at 14:52
  • Mock file storage? It should be used for testing — not for real sessions. Use `session.storage.native`. – Elnur Abdurrakhimov Sep 26 '12 at 17:08
  • The problem is that i need the local symfony session to have the same session save path from the master session. And in the Local value i have **C:/wamp/www/Symfony/symproject/app/cache/dev/sessions** and the master value i have **c:/wamp/tmp**. This is not ok for authentication reasons (i'm using simple saml php). What should i do in this case? I didn't have this problem on Symfony 2.0. The Local session save path value was not changed by symfony. – Fonsini Sep 26 '12 at 17:19
  • Just set `framework:session:save_path` in `config.yml` to whatever you want. – Elnur Abdurrakhimov Sep 26 '12 at 18:28
  • I have my project on git. And the save path might be diferent depending if im on my local machine or not. I dont think its a good practice to force this. Is it possible to get the value from master sesion? The value from php.ini? In symfony 2.0 this was out of box. – Fonsini Sep 26 '12 at 21:50
6

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.

Community
  • 1
  • 1
Fonsini
  • 693
  • 8
  • 18