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?
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 %}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With