I have some controller with method_1(). In this method I call method_2(). In method_2() I have (try... catch) - block with defined flashMesseges and redirect.
$this->flashMessenger()->addErrorMessage("There are errors.");
return $this->redirect()->toRoute('home');
But it not work. But if I write as
$this->redirect()->toRoute('home');
$this->flashMessenger()->addErrorMessage("There are errors.");
All OK. In method_1() code
$this->flashMessenger()->addErrorMessage("There are errors.");
return $this->redirect()->toRoute('home');
good working. I don't understand. Can anybody help me?
Class A - redirect not working. And message add to session.
class A {
public function manageAction()
{
$view = new ViewModel();
$form = $this->getForm();
$form = $this->fillForm($form);
$view->form = $form;
return $view;
}
public function fillForm($form)
{
try {
// ...
} catch (\Exception $e) {
$this->flashMessenger()->addErrorMessage("Error");
return $this->redirect()->toRoute('home');
}
return $form;
}
}
Class B - redirect working. And message printed.
class B {
public function manageAction()
{
$view = new ViewModel();
$form = $this->getForm();
$form = $this->fillForm($form);
$view->form = $form;
return $view;
}
public function fillForm($form)
{
try {
// ...
} catch (\Exception $e) {
$this->redirect()->toRoute('home');
$this->flashMessenger()->addErrorMessage("Error");
}
return $form;
}
}
Why and how it work?
The Plugin FlashMessenger , send your message to a waiting pool ( Through FlashMessenger Zend MVC Plugin ) which will be displayed on another page request ( Through ViewHelper FlashMessenger ) .
There are 4 types of messages that you can integrate with the Bootstrap Notifications ( error, info , default , success ) .
Now let's practice
In Action within the Controller , you must enter your message and your brand :
In View ( .phtml ) , you only need to use :
In View , if using Bootstrap :
Now 's a hack , if you want to view the FlashMessages on the screen without resquest ou redirect page ( Ideal for form errors , which you do not redirects or AJAX to another page ) , use renderCurrent and clear.
echo $flash->renderCurrent('error', array('alert', 'alert-dismissible', 'alert-danger'));
If you want to deepen better at it, follow the links Official Zend 2 documentation , gives a tried on available methods , will help a lot :
VIEW -> http://framework.zend.com/manual/current/en/modules/zend.view.helpers.flash-messenger.html
CONTROLLER -> http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-flashmessenger