3

我正在使用 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);
            }
        }
    }

在管理员/login.ctp 中:

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

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

但是无法在 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);

        }
    }
} 

在经销商/login.ctp 中:

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

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

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

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

4

2 回答 2

3

确保您尚未使用 Session 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()回调并确保您没有闪烁上述消息。如果是,它会覆盖前一个。

于 2015-12-03T12:04:46.563 回答
1

您好,我认为您忘记回显您的会话消息,请在您的 ctp 文件中写如下

echo $this->Session->flash(); 
于 2015-12-03T12:13:41.620 回答