簡體   English   中英

為什么我的flash信息不顯示? (Cake PHP 2.0)

[英]Why won't my flash message show? (Cake PHP 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(); ?>

正確的閃存消息顯示在調試中,但不顯示在視圖上。 我錯過了什么? PS這不是因為?>之后的空間。

編輯:我發現CakePHP由於某種原因在會話組件之前調用會話助手。 仍在試圖弄清楚如何解決它。

創建flash消息的簡單方法是在app / view / element目錄中創建ctp文件

嘗試

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

您需要在視圖中定義flash('auth')以查看authentication session flash messages.

    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') ?>

我想是因為你在這里有錯誤:

<?php echo "Flash:" ?>

你錯過了分號

<?php echo "Flash:"; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM