cakephp 版本 2.2.2 中的持久性 Flash 消息
Persistent Flash Message in cakephp version 2.2.2
在我看来我有:
<?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());
}
问题是即使在成功登录后我的闪现消息仍然存在。
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
(
)
)
)
)
被迫添加更多细节,因为我的 post 主要是代码。当我使用错误的密码时,它不会显示错误消息。它在我注销后显示错误消息。
Flash 消息在至少一次重定向后有效,这就是您收到此消息的原因。当您访问此 login
操作以显示登录表单时 flash message
设置但未打印。成功登录后,由于一次重定向,这条旧的闪存消息被打印出来。您需要像这样编写登录方法:
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'));
}
}
}
在我看来我有:
<?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());
}
问题是即使在成功登录后我的闪现消息仍然存在。
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
(
)
)
)
)
被迫添加更多细节,因为我的 post 主要是代码。当我使用错误的密码时,它不会显示错误消息。它在我注销后显示错误消息。
Flash 消息在至少一次重定向后有效,这就是您收到此消息的原因。当您访问此 login
操作以显示登录表单时 flash message
设置但未打印。成功登录后,由于一次重定向,这条旧的闪存消息被打印出来。您需要像这样编写登录方法:
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'));
}
}
}