Session->flash 在 CakePHP 中不显示会话消息

Session->flash is not showing session message in CakePHP

我正在使用 CakePHP 2.6.7 并复制代码以从一个控制器向另一个控制器显示 flash 消息,但它在第二个控制器中不起作用。

在 AdminsController 中:

   function login() {
        $this->loadModel('Admin');
        $this->layout = "admin-login";
        // if already logged in check this step
        if ($this->Auth->loggedIn()) {
            return $this->redirect('dashboard'); //(array('action' => 'deshboard'));
        }
        // after submit login form check this step
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                // pr($this->Auth); exit;
                if ($this->Auth->user('status') == 'active') {
                    // user is activated
                    $this->Admin->id = $this->Auth->user('id');
                    $this->Admin->saveField("loggedIn", 1);
                    return $this->redirect($this->Auth->redirectUrl());
                } else {
                    // user is not activated
                    // log the user out
                    $msg = '<div class="alert alert-error">
                           <button type="button" class="close" data-dismiss="alert">×</button>
                           <strong>You are blocked, Contact with Adminstrator</strong>
                        </div>';
                    $this->Session->setFlash($msg);
                    return $this->redirect($this->Auth->logout());
                }
            } else {
                $msg = '<div class="alert alert-error">
                           <button type="button" class="close" data-dismiss="alert">×</button>
                           <strong>Incorrect email/password combination. Try Again</strong>
                        </div>';
                $this->Session->setFlash($msg);
            }
        }
    }

在admins/login.ctp:

<?php echo $this->Session->flash(); ?>

当我输入错误的电子邮件或密码时,它会显示错误消息。 证明:http://jegeachi.com/admins/login

但 SAME 任务无法在 ResellersController 中完成。这是控制器代码:

function login() {
    $this->layout = 'public-login';
    $this->loadModel('Reseller');
        // if already logged in check this step
    if ($this->Auth->loggedIn()) {
            return $this->redirect('profile'); //(array('action' => 'deshboard'));
        }
        // after submit login form check this step
        if ($this->request->is('post')) {

            if ($this->Auth->login()) {

                return $this->redirect($this->Auth->redirectUrl());
            } else {

                $msg = '<div class="alert alert-error">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>Incorrect email/password combination. Try Again</strong>
            </div>';
            $this->Session->setFlash($msg);

        }
    }
} 

在Resellers/login.ctp:

 <?php echo $this->Session->flash(); ?>

当由于错误的电子邮件或密码导致登录失败时,不会显示。

证明:http://jegeachi.com/resellers/login

这是一个奇怪而复杂的问题。相同的代码在控制器中工作,但在其他控制器中不起作用。有什么想法吗?

确保您尚未使用会话快闪消息。 如果您的 app/View/Layouts/public-login.ctp 布局中有如下内容,则可能会发生这种情况:

<?php $this->Session->flash(); ?>

或者在你的 view/view 块中的某处。


检查 HTML 输出,我发现以下内容而不是预期的错误消息:

    <div class="alert alert-danger display-hide">
        <button class="close" data-close="alert"></button>
        <span> Enter Email and password. </span>
    </div>

检查您的 beforeRender() 回调并确保您没有闪烁上述消息。如果你是,它会覆盖之前的。

您好,我想您忘记回显您的会话消息 请像下面这样写 在你的 ctp 文件中

echo $this->Session->flash();