cakepp 2.2.2版中的持久性Flash消息



在我看来,我有:

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

在我的UsersController中,我有:

public function login() {
$this->Session->delete('Flash.auth');
$this->Session->delete('Message.flash');
$this->Session->delete('auth');
$this->Session->delete('Message.auth');
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect(array('controller' => 'users', 'action' => 'index')));
} else {
$this->Session->setFlash('Invalid username or password, please try again', null, null);
}
}
public function logout() {
$this->defaultTextView();
return $this->redirect($this->Auth->redirect(array('controller' => 'users', 'action' => 'login')));
}
function index($id = null) {    
$this->Session->delete('Message.flash');
$this->Session->destroy('Message.flash');
$user_id_sess = $this->Session->read('Auth.User.id');
$this->defaultAdminViewHere();
$this->set('user_id_sess', $this->Session->read('Auth.User.id'));
$this->User->id = $id;
$this->set('user', $this->User->read());    
}

问题是,即使在成功登录后,我的flash消息仍然存在。

pr($this->Session->read(((;收益:

Array
(
[Config] => Array
(
[userAgent] => adcb84540c454620867cea3249a69ca1
[time] => 1544388026
[countdown] => 10
)
[Message] => Array
(
[flash] => Array
(
[message] => Invalid username or password, please try again
[element] => default
[params] => Array
(
)
)
)
)

被迫添加更多细节,因为我的帖子主要是代码。当我使用错误的密码时,它不会显示错误消息。它会在我注销后显示错误消息。

flash消息在至少一次重定向后即可工作,这就是您收到此消息的原因。当您访问此login操作以显示登录表单flash message集但无法打印时。成功登录后,由于一次重定向,这个旧的flash消息被打印出来。您需要编写这样的登录方法:

public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect(array('controller' => 'users', 'action' => 'index')));
} else {
$this->Session->setFlash('Invalid username or password, please try again', null, null);
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
}
}

最新更新