views:

412

answers:

1

Hi, I am new to Zend framework and I have a problem. I created a controller abstract class which implements the functions 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 am able to use $this->AddError($error); And then I render $error in layout. So I want not to deal with flashMesenger in every controller.

but I have to execute the activateErrors when each action is executed.

for example

I have an controller test

class TestController 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 that I could execute this activateErrors in each action for every controller at the end of action without duplicating the code.

I mean I do not want to include this code at every action. Maybe there is a way to include it in my abstract class MyController.

Anybody any Idea?

thanks

+1  A: 

Hi,

What about using a postDispatch hook, in your parent MyController ?

Quoting that page :

Zend_Controller_Action specifies two methods that may be called to bookend a requested action, preDispatch() and postDispatch().
These can be useful in a variety of ways: verifying authentication and ACL's prior to running an action (by calling _forward() in preDispatch(), the action will be skipped), for instance, or placing generated content in a sitewide template (postDispatch()).

Maybe this might do the trick ?

Pascal MARTIN