8

I am setting a flash message in my controller when rendering a twig template. If there is a post action, I would like to redirect to the same page, but change the flash message.

if ($request->isMethod('POST')) {
    ...
    ...

    $this->get('session')->getFlashBag()->clear(); // Does not work
    $this->get('session')->getFlashBag()->all();   // Does not work

    $request->getSession()->getFlashBag()->set('user-notice', $flash_message2);

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


$this->get('session')->getFlashBag()->set('user-notice', $flash_message1);

return $this->render(....

But the problem is that the displayed flash messages is the $flash_message1, and should be $flash_message2.

When trying to use add instead of set, I can see them both. I tryied to use the Symfony2 clear() and all() functions: http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.html but nothing changed.

Any idea? Thank you !!!

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • could you please include the method's relevant code completely and maybe add how your attempt with `clear()` and `set()` looked? – Nicolai Fröhlich Nov 19 '13 at 13:42
  • `problem is that the displayed flash messages is the 1st one.` ... what message gets returned if you send a POST request? ... $flash_message or $flash_message **2** ? – Nicolai Fröhlich Nov 19 '13 at 13:47
  • When using the post, flashMessage2 needs to be displayed, but instead of this one, the flashMessage1 is displayed. – Milos Cuculovic Nov 19 '13 at 13:51

3 Answers3

16

To clear all flash messages use the following code:

$this->get('session')->getFlashBag()->clear();
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Chaitenya
  • 329
  • 2
  • 10
  • 2
    Why was this downvoted ? An explanation would help people like me, reading this, understand what's going on. – conradkleinespel Nov 06 '15 at 15:37
  • 2
    Upvoted, there's absolutely no reason this should be downvoted. The only thing to note is that this clears the ENTIRE flashbag and not a specific one like 'user-notice'. – Prof Feb 05 '16 at 06:15
15

Use...

$flashBag = $this->get('session')->getFlashBag();
$flashBag->get('user-notice'); // gets message and clears type
$flashBag->set('user-notice', $flash_message2);

... after your isPost() condition.

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • Ahhh, I know what's the problem. When I am using redirect, I am redirecting to the same page, so even if I set the flashMessage2, an the end the flashMessage1 will be set because I am redirecting to the same page. Any idea on how to fix that ? – Milos Cuculovic Nov 19 '13 at 13:55
  • Thanks, but this did not fixed teh problem. The `$flashBag->has('user-notice')` is allwaays false. – Milos Cuculovic Nov 19 '13 at 14:03
  • have you cleared your cache and your browser cache ? Updated my answer - just use `get()` to clear the message. It has to be unset afterwards ... see [FlashBag Line 97](https://github.com/symfony/HttpFoundation/blob/master/Session/Flash/FlashBag.php#L97). – Nicolai Fröhlich Nov 19 '13 at 14:11
  • 1
    Yes, both caches are cleared. About your new answer, the problem is how to know if I have to set the 1st or 2nd message. In both cases, I am comming from an redirect, so not from post. Should I use a simple render instead of redirect ? – Milos Cuculovic Nov 19 '13 at 14:52
2

One easy way to delete all flash messages is as follows:

// clear all messages from FlashBag
$flashBag = $this->get('session')->getFlashBag();
foreach ($flashBag->keys() as $type) {
    $flashBag->set($type, array());
}

This works just fine in Symfony 2.4, and probably all other recent versions.

likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
  • That is not easy! The easy way is this: `$this->get('session')->getFlashBag()->clear();` – Peyman Mohamadpour Dec 17 '16 at 08:43
  • @Trix There used to be an issue with exactly what you suggested two years ago. Have another look at the question: Milos is asking what to do when it does not work that way. – likeitlikeit Dec 21 '16 at 20:12