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

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

5 Comments

  1. Nate
    Posted February 28, 2006 at 4:30 pm | Permalink

    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.

  2. Posted February 28, 2006 at 4:45 pm | Permalink

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

  3. Posted June 18, 2006 at 5:41 pm | Permalink

    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.

  4. Posted June 18, 2006 at 5:43 pm | Permalink

    parser was hungry:

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

  5. Posted July 20, 2013 at 10:19 pm | Permalink

    Awesome! Its genuinely remarkable post, I have got much clear idea
    on the topic of from this paragraph.


Post a Comment

Required fields are marked *
*
*

%d bloggers like this: