Session 会话->;flash未在CakePHP中显示会话消息
我使用的是CakePHP2.6.7,并复制了代码以将flash消息从一个控制器显示到另一个控制器,但它在第二个控制器中不起作用 在AdminsController中:Session 会话->;flash未在CakePHP中显示会话消息,session,cakephp,flash-message,cakephp-2.6,cakephp-2.x,Session,Cakephp,Flash Message,Cakephp 2.6,Cakephp 2.x,我使用的是CakePHP2.6.7,并复制了代码以将flash消息从一个控制器显示到另一个控制器,但它在第二个控制器中不起作用 在AdminsController中: function login() { $this->loadModel('Admin'); $this->layout = "admin-login"; // if already logged in check this step if ($thi
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);
}
}
}
函数登录(){
$this->loadModel('Admin');
$this->layout=“admin login”;
//如果已登录,请检查此步骤
如果($this->Auth->loggedIn()){
返回$this->redirect('dashboard');/(数组('action'=>'deshboard');
}
//提交登录表单后,请检查此步骤
如果($this->request->is('post')){
如果($this->Auth->login()){
//pr($this->Auth);退出;
如果($this->Auth->user('status')=='active'){
//用户已激活
$this->Admin->id=$this->Auth->user('id');
$this->Admin->saveField(“loggedIn”,1);
返回$this->redirect($this->Auth->redirectUrl());
}否则{
//用户未激活
//将用户注销
$msg='1
×
您被阻止,请与管理员联系
';
$this->Session->setFlash($msg);
返回$this->redirect($this->Auth->logout());
}
}否则{
$msg='1
×
电子邮件/密码组合不正确。请重试
';
$this->Session->setFlash($msg);
}
}
}
在admins/login.ctp中:
<?php echo $this->Session->flash(); ?>
<?php echo $this->Session->flash(); ?>
当我键入错误的电子邮件或密码时,它会显示错误消息。
证明:
但同样的任务不能在经销商控制器中完成。以下是控制器代码:
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);
}
}
}
函数登录(){
$this->layout='public login';
$this->loadModel(“经销商”);
//如果已登录,请检查此步骤
如果($this->Auth->loggedIn()){
返回$this->redirect('profile');/(数组('action'=>'deshboard');
}
//提交登录表单后,请检查此步骤
如果($this->request->is('post')){
如果($this->Auth->login()){
返回$this->redirect($this->Auth->redirectUrl());
}否则{
$msg='1
×
电子邮件/密码组合不正确。请重试
';
$this->Session->setFlash($msg);
}
}
}
在经销商/login.ctp中:
<?php echo $this->Session->flash(); ?>
<?php echo $this->Session->flash(); ?>
由于错误的电子邮件或密码导致登录失败时,不会显示
证明:
这是一个奇怪的问题。相同的代码在控制器中工作,但在其他控制器中不工作。有什么想法吗?确保您尚未使用会话闪存消息。 如果您的
应用程序/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()
回调,确保没有闪烁上述消息。如果是,它将覆盖上一条。您好,我想您忘记回显会话消息了
请像下面这样写
在您的ctp文件中
echo $this->Session->flash();
我正在使用cakephp 2.6.7并在admins controller和分销商controller中加载会话。我检查了一下,奇怪的是:我将所有文件下载到本地pc中。会话消息显示在本地主机中。这真的很令人沮丧。我现在得到的只是空白页上的“错误”信息。我只是试着用echo“错误”;出口用于测试。现在我编辑我的代码如上所述。现在你看不到带有错误电子邮件密码的flash消息。