The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Flash messages not working as expected

cain.martin's Avatar


15 Apr, 2015 02:19 AM

I've been trying to implement flash messages in an app I'm building but running into a few fundamental issues.
I'm using native PHP sessions, so I start the session with session_start().
I then set a message using the flash command. $app->flash('error', 'This is a test error');.
I tried to access this via a twig template - and nothing. I have not been able to find a good explanation on how I should be accessing the variables via twig - so I tried everything. {{ flash.error }} and {{ error }} and even {{ slim.flash.error }}.
I was getting nothing - so I decided I would check the session variable in PHP to see what was going on.

$app->get('/test2', function() use ($app) {
    $app->flash('error', 'Testing');
    echo "<pre>";
    echo "</pre>";
    echo $app->twig->render('test2.html');

And I could see that it is getting written. What seemed odd though - is it only appears in the session variable on refresh. If I change the text, reload the page, the session variable holds the previous data. It's only on refresh that it is updated. So I tried using flashKeep. Nothing is written into that session variable - ever.

So I'm completely stumped - any ideas on what might be happening here?

  1. 1 Posted by cain.martin on 15 Apr, 2015 02:30 AM

    cain.martin's Avatar

    I should add - I believe the $app->flash('var', 'message'); method will set the variable for the next request (so it will be available for a redirect) - that PROBABLY explains the reload behaviour. So using $app->flashNow (which would be for the current request) would seem to be the go - but it just trashes the slim.flash SESSION array - leaving it empty.

  2. 2 Posted by Yannick on 17 Jun, 2015 10:40 AM

    Yannick's Avatar

    For what it's worth, I added a global var to my Twig environment :

    $app->twig->getEnvironment()->addGlobal('flash', $_SESSION['slim.flash']);

    and then I was able to access flash messages from within my templates the regular way :


    I hope this helps!

  3. 3 Posted by Peter on 27 Feb, 2018 07:13 AM

    Peter's Avatar

    Thanks for your article check the latest app for watching the live tv

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac