4
votes

In AppController:

public $helpers=array("Session","Html","Form");
public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'MainPages', 'action' => 'home'),
        'logoutRedirect' => array('controller' => 'MainPages', 'action' => 'front')
    )
);

In MainPagesController:

public function front()
{

    $this->Session->setFlash('Your stuff has been saved.');
    debug($this->Session->read('Message'));
    //etc...

In default layout (default.ctp)

<div id="content">
    <?php echo "Flash:" ?>
    <?php echo $this->Session->flash(); ?>

The correct flash message shows in the debug but not on the view. What am I missing? PS It's not because of space after the ?>.

Edit: I have discovered that CakePHP is calling the session helper before the session component for some reason. Still trying to figure out how to fix it.

4
Is there <?php echo $this->Session->flash(); ?> present in your default.ctp? - عثمان غني
Yes, see third code box. - Nick Manning
The component is never called after the helper. either only the helper, or the component and then the helper! But if you have trouble with displaying the flash messages, try $this->disableCache() in the controller. Some browsers are known to cache too harsh and thus preventing the update (as flashmessage) to be properly displayed. - mark

4 Answers

2
votes

Simple way to create flash messages is to create their ctp file in app/view/element dir

0
votes

Try

<div id="content">
    <?php echo "Flash:" ?>
    <?php echo $this->Session->flash('auth'); ?> 
    <?php echo $this->Session->flash(); ?>

You need to define flash('auth') in your view to see authentication session flash messages.

0
votes
    My setflash works in this way..
    Keep this in the App controller

    function setFlash($message, $type = 'blue') {
           //what ever the color you need..
            $arr = array('red' => 'red', 'green' => 'green', 'yellow' => 'yellow', 'gray' => 'gray', 'blue' => 'blue');
            $this->Session->setFlash($message, 'default', compact('class'), $arr[$type]);
        }

    and then call it from the controller action where you need to make the flash message as below..

    $this -> setFlash("This is flash message","red");

    now you need to make the flash in the layout(if you are using) or in your ctp file..

    <?php echo $this->Session->flash() ?>
    <?php echo $this->Session->flash('red') ?>
-2
votes

I think it is because you have an error here:

<?php echo "Flash:" ?>

You are missing the semi-colon

<?php echo "Flash:"; ?>