4

在 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')
    )
);

在 MainPagesController 中:

public function front()
{

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

在默认布局中 (default.ctp)

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

正确的闪存消息显示在调试中,但不在视图中。我错过了什么?PS这不是因为?>之后的空间。

编辑:我发现 CakePHP 出于某种原因在会话组件之前调用了会话助手。仍在试图弄清楚如何解决它。

4

4 回答 4

2

创建 Flash 消息的简单方法是在 app/view/element 目录中创建它们的 ctp 文件

于 2013-08-24T10:42:25.823 回答
0
    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') ?>
于 2013-03-05T09:18:03.917 回答
0

尝试

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

您需要flash('auth')在视图中定义才能看到authentication session flash messages.

于 2013-02-18T08:01:13.270 回答
-2

我认为这是因为您在这里有一个错误:

<?php echo "Flash:" ?>

您缺少分号

<?php echo "Flash:"; ?>
于 2013-02-17T20:57:29.610 回答