3

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...

2
  • 1
    Check your sessions bag using the Sf2 Profiler, in the Request panel, you should be able to check what are the data inside your bag. Commented Sep 12, 2013 at 15:48
  • Try to remove $session->save(); Commented Sep 12, 2013 at 16:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right I'd a modal with FriendsOfSymfony login which was doing app.session.flashbag.all() then all my messages were removed from the flasbag. Thanks for your help guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.