我想知道是否可以使用flash messenger而无需重定向?例如。登录失败后,我想继续显示表单,不需要重定向。
public function loginAction() {
$form = new Application_Form_Login();
...
if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getParams())) {
$authAdapter = new Application_Auth_Adapter($form->getValue('username'), $form->getValue('password'));
if ($this->auth->authenticate($authAdapter)->isValid()) {
...
} else {
// login failed
$this->flashMessenger->addMessage('Login failed. You may have entered an invalid username and/or password. Try again');
}
}
$this->view->form = $form;
}
答案 0 :(得分:8)
您可以使用$ this-> flashMessenger-> getCurrentMessages();无需重定向即可检索Flash消息。 例如:
$this->view->messages = array_merge(
$this->_helper->flashMessenger->getMessages(),
$this->_helper->flashMessenger->getCurrentMessages()
);
$this->_helper->flashMessenger->clearCurrentMessages();
答案 1 :(得分:3)
当然,你可以。但我通常会将auth-failure消息附加到表单本身。实际上,即使表单级验证失败,我也希望显示类似“请注意下面的错误”的内容。所以,我分开处理这两个案件:
public function loginAction()
{
$form = new Application_Form_Login();
if ($this->getRequest()->isPost()){
if ($form->isValid($this->getRequest()->getPost())){
$username = $form->getValue('username');
$userpass = $form->getValue('userpass');
$adapter = new Application_Model_AuthAdapter($username, $userpass);
$result = $this->_auth->authenticate($adapter);
if ($result->isValid()){
// Success.
// Redirect...
} else {
$form->setErrors(array('Invalid user/pass'));
$form->addDecorator('Errors', array('placement' => 'prepend'));
}
} else {
$form->setErrors(array('Please note the errors below'));
$form->addDecorator('Errors', array('placement' => 'prepend'));
}
}
$this->view->form = $form;
}