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?