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

标签 session cakephp flash-message cakephp-2.6 cakephp-2.x

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

在 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 中完成。这是 Controller 代码:

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

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

最佳答案

确保您还没有使用 Session 闪现消息。 如果您的 app/View/Layouts/public-login.ctp 布局类似于以下内容,则可能会发生这种情况:

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

或在您的 View / View block 中的某处。


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

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

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

关于Session->flash 在 CakePHP 中不显示 session 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064599/

相关文章:

c# - 如何将 session 变量与 C# 中的字符串进行比较?

php - HDD 上的旧 PHP session 文件

cakephp - 在 CakePHP 中保存记录时使用 'on duplicate key update'

mysql - 数据库错误 PDOException 错误 : SQLSTATE[HY000] [1045]

JavaScript 和 CakePHP : how to get this simple script working?

python - 如何在 2 个不同的 app.route 中使用 Flask Flash 消息

php - 如何限制成员(member)使用 session 看不到admin_page.php

css - 如何检查cakephp中的flash消息集是否为空

ruby-on-rails - 自定义 Rails flash 通知,无需 Bootstrap

javascript - Node.js session 文件存储将中止所有后续请求