Search code examples
sessioncakephpflash-message

CakePHP: How can I read in controller the string value of a flash message if set?


I need to know this so I can append messages (flashes) if needed.

This way I can give the user the full feedback and avoid one flash being overwritten (in a redirect, for ex, where the last controller, usualy, can do that).

I read the documentation and I did't find any option to be given in setFlash() in order to require this appending.

I know there is a Session::read(), but I do not know what key to search for..

Thank you!


Solution

  • What you are looking for is:

    $this->Session->read('Message');
    

    Message is the key that hold the session messages for the current user, be it flash messages or auth messages. A simple pr($this->Session->read()) will give you output similar to:

    Array(
        ['Auth'] => array(
            ... your auth keys and values here
        ),
        ['Message'] => array(
            ['flash'] => ... your current flash message array (if any)
            ['auth'] => ... your current auth message array (if any)
        )
    )
    

    Although I am not sure why you are worried. When you do

    $this->Session->setFlash('your message');
    $this->redirect('/');
    

    Even if you do have a redirect, the session message will persist and will display on the redirected page. You just need to make sure you are outputting the flash messages.