Search code examples
phpzend-framework

Possible to use FlashMessenger without redirect?


I wonder if its possible to use flash messenger without redirect? eg. After a failed login, I want to continue to display the form, no redirect required.

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;
}

Solution

  • You can retrieve flash messages without redirect using $this->flashMessenger->getCurrentMessages(); Example:

    $this->view->messages = array_merge(
        $this->_helper->flashMessenger->getMessages(),
        $this->_helper->flashMessenger->getCurrentMessages()
    );
    $this->_helper->flashMessenger->clearCurrentMessages();