Symfony 2.4 app.session.flashbag.all() returns empty value
php, session, symfony, twig
Solution
I've just stumbled on the same problem and found this: Symfony2 FlashBag stopped working after upgrade to 2.4?
If the other subject doesn't answer your question you might want to try and dump your flashbag to see it's structure. Do do so, try adding this in your twig template:
{% set array = app.session.flashbag.all %}
{% dump(array) %}
You might be surprised by what comes out, at least I've been:
array(1) { ["test"]=> array(1) { [0]=> string(28) "Ceci est un test de flashbag" } }
Which means that you do have messages in your flashbag but you can't get the content the right way as it's in a second array. My solution:
{% for tag, line in array %}
{% for underline in line %}
<div class="{{tag}}">{{ underline }}</div>
{% endfor %}
{% endfor %}
Hope this helps
Problem
I have a trouble with using the flashbag messages. My case is quite simple : My code Editing a page using a form : ``` # src/Namespace/MyBundle/Resources/views/Edit/form.html.twig <form action="{{ path('form_url_save', {'id': id }) }}" method="POST"> {{ form_widget(form) }} </form> ``` Save the data form in a database via a controller : ``` # src/Namespace/MyBundle/Controller/EntityController.php public function saveAction(Request $request, Entity $entity = null) { try { if (!$entity) { $entity = new Entity(); } $form = $this->createForm(new EntityType(), $entity); if ($request->getMethod() == 'POST') { $form->submit($request); if ($form->isValid()) { // Entity manager $em = $this->getDoctrine()->getManager(); // Persist data $em->persist($form->getData()); // Saving process $em->flush(); // Add flashbag message $this->get('session')->getFlashBag()->add('success', 'The backup was done successfully')); } else { throw new \Exception($form->getErrorsAsString()); } } } catch (\Exception $e) { $this->get('session')->getFlashBag()->add('error', $e->getMessage()); } return $this->redirect('home_page_url'); } ``` Display a successfull message on front : ``` # app/Resources/views/front.html.twig <html> <head></head> <body> <div class="main"> {% set flashbag = app.session.flashbag.all %} {% if flashbag is not empty %} <div class="messages-container"> {% for type, messages in flashbag %} {% for message in messages %} <div class="alert alert-{{ type }}"> {{ message }} </div> {% endfor %} {% endfor %} </div> {% endif %} <div class="content"> // My content </div> </div> </body> </html> ``` How my theme is organized ? ``` app/Resources/views/front.html.twig |__ src/Namespace/MyBundle/Resources/views/Edit/form.html.twig // extends front.html.twig ``` My trouble : - `app.session.flashbag.all` in `front.html.twig` ==> Flashbag is empty - `app.session.flashbag.all` in `form.html.twig` ==> Flashbag is good and had the success message So why i can't put the code in the `front.html.twig` ?