Problems with Zend FlashMessenger
I'm new to Zend platform and I have a problem. I have created an abstract controller class that implements features like:
protected function AddError($message) {
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$flashMessenger->addMessage($message);
$this->view->Errors = $flashMessenger->getMessages();
}
protected function activateErrors()
{
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$this->view->Errors = $flashMessenger->getMessages();
}
So, for each controller, I can use $ This-> AddError ($ error); And then I throw $ error in the layout. So I don't want to deal with flashMesenger in every controller.
but i have to execute activateErrors when every action is executed.
eg
I have a control test
TestController class extends MyController {
public function indexAction() {
$this->AddError("Error 1");
$this->AddError("Error 2");
$this->activateErrors();
}
public function index1Action() {
$this->AddError("Esdsd 1");
$this->AddError("sddsd 2");
$this->activateErrors();
}
}
Is there a way I could do this activateErrors in every activity for every controller at the end of the activity without duplicating code.
I mean, I don't want to include this code in every action. Maybe there is a way to include it in my abstract class MyController.
Any idea?
thank
How about using the postDispatch hook on the parent MyController
?
Quoting this page:
Zend_Controller_Action
specifies two methods that can be called on the bookend for the requested action,preDispatch()
andpostDispatch()
.
They can be useful in a variety of ways: checking authentication and ACLs before executing an action (by calling_forward()
inpreDispatch()
, the action will be skipped), for example, or placing the generated content in a sitewide template (postDispatch()
).
Maybe this can do the trick?
source to share
I have indeed implemented a FlashMessenger extension that provides a lot of the functionality you are looking for.
source to share