为什么我的flash信息不显示? (Cake PHP 2.0)

时间:2013-02-17 17:03:39

标签: php cakephp cakephp-2.0

在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(); ?>

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

编辑:我发现CakePHP由于某种原因在会话组件之前调用会话助手。仍在努力弄清楚如何修复它。

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

尝试

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

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

答案 2 :(得分: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') ?>

答案 3 :(得分:-2)

我认为这是因为你在这里有错误:

<?php echo "Flash:" ?>

您缺少分号

<?php echo "Flash:"; ?>