Some changes for Session->flash() and Session->setFlash()

Published on and tagged with cakephp  feature

In the coming release of CakePHP (RC6) there are some changes in the aforementioned functions. The changes in Session->flash() could break existing code as the function signature has changed (but I think most people use this function without parameters). The old signature was:

function flash($css = 'message', $return = false)

And the new signature is:

function flash($key = 'flash')

The signature of Session->setFlash() has changed, too. Some new parameters were added. So the new signature is:

function setFlash($flashMessage, $layout = 'default', $params = array(), $key = 'flash')

$layout allows you to specify a layout used for the flash message, and with $params you can send data to that layout. The $key parameter makes it possible to store different flash messages in the session. You retrieve them with the new $key parameter of Session->flash().

4 comments baked

  • Nate

    The “old” signature that you show wasn’t really old, it was only there for a day or two, just something we were test piloting.

    The new parameters for both methods are completely compatible with existing code, and should produce the same result.

  • cakebaker

    @Nate: Thanks for the correction, it was my fault.

  • luffy

    Flash messages implementation in Cakephp is rather puzzling then clear. There are number of functions in Controller and in SessionComponent. Controller flash functions redirects you, in SessionComponent functions if you don’t specify aditional parameter for $layout as “null”, View is renedered and then send through session. While it’s all nice and funky i don’t see the reasons for such a behaviour to be default. It destorts the functionality and gives (somewhat)wrong prespective.
    With “flash” messages, basicaly, what you need is to be able to send message between two actions. For that matter collector in Controller class should be used. The collector is represented as an attribute of type Array. Before Controller action is taken “flash” message is restored from session and stored in Controller::flash. After the action if new flash message was set it should go to session and be there untill next Controller/Action is runned.

    At the end you have Controller::flash attribute as an array.
    Usage in controller is: $this->flash[‘notice’] = ‘The Post has been saved’;
    Then in view: controller->flash[‘notice’] ?>
    You can create a simple element for a layout if you want to handle different flash message representations where you decide by Controller::flash key value how to represent it. Be it ‘notice’ or ‘error’ or whatever you want.

  • luffy

    parser was hungry:

    Then in view: echo $this->controller->flash[‘notice’];

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License