Flash Message disappears on redirect in Symfony 2.1
Asked Answered
M

2

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?

Machinate answered 26/9, 2012 at 14:11 Comment(0)
M
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.

Machinate answered 27/9, 2012 at 10:54 Comment(0)
T
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 %}
Telangiectasis answered 26/9, 2012 at 14:32 Comment(5)
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.Machinate
Mock file storage? It should be used for testing — not for real sessions. Use session.storage.native.Telangiectasis
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.Machinate
Just set framework:session:save_path in config.yml to whatever you want.Telangiectasis
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.Machinate
M
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.

Machinate answered 27/9, 2012 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.