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

+2


source to share


2 answers


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()

and postDispatch()

.
They can be useful in a variety of ways: checking authentication and ACLs before executing an action (by calling _forward()

in preDispatch()

, the action will be skipped), for example, or placing the generated content in a sitewide template ( postDispatch()

).

Maybe this can do the trick?

+1


source


I have indeed implemented a FlashMessenger extension that provides a lot of the functionality you are looking for.



0


source







All Articles