MSánchez
MSánchez

Reputation: 545

Symfony2 flashBag empty array

I've read lots of post here in Stackoverflow about this topic but still I had no success.

I'm working with Symfony version 2.2.3 and currently I'm trying to make work the flashbag messegaes in my project, but for some reason when I'm receiving an empty array in my Twig template.

Here is my Controller code:

public function addProductAction($id) {
        $session = $this->container->get('session');

        // *** Some nive stuff here with session parameters **

        $session->set('products', $products);

        // Here it is my flashbag code sort like in symfony2 documentation

        $session->getFlashBag()->add('notice', 'Your changes were saved!');
        $session->save();

        return $this->redirect($this->getRequest()->headers->get('referer'));
}

And here my Twig code:

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="flash-notice">
        {{ flashMessage }}
    </div>
{%else%}
    <div class="flash-notice">
        NO MESSAGES
    </div>
{% endfor %}

I've done it just like people say in lots of post in the web, but with no luck... Any idea? As I think I'm only doing ONE request, can't understand why it's not working...

Upvotes: 3

Views: 3327

Answers (1)

Cyprian
Cyprian

Reputation: 11364

Probably you have another get('notice') before this one. get method (from FlashBag class) works in that way it removes entry and return it (so it's more like "pop" than "get"). If you want just get an entry without removing it from flash bag use peek method.

And about your question - make sure that you don't call get('notice') before.

Upvotes: 1

Related Questions