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

时间:2015-12-03 11:18:07

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

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

在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

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

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

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

2 个答案:

答案 0 :(得分:3)

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

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

或视图/视图块中的某个位置。

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

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

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

答案 1 :(得分:1)

您好我觉得您忘记回复您的会话消息了 请写下面的内容 在你的ctp文件中

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