我正在使用 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/