0
votes

I'm new to Zend framework and can't find a clear answer on this. I essentially want some code to execute after a controller's logic for a page, but before the layout and view are rendered.

For example, I want to auto-refresh the flash messages and provide them to the layout/view automatically, so that I don't need to do so in every controller. This obviously needs to happen after the controller code has executed since it might add messages.

$this->view->messages = $this->_helper->flashMessenger->getMessages();
2
What do you mean by auto-refresh the flashMessenger? It's always fresh!markus
I mean to re-assigned the result of getMessages to the view->messageshelion3
You shouldn't need to do that, instead use a view helper to display the flash messages.markus

2 Answers

1
votes

The easiest way to do this is with a controller plugin, see http://framework.zend.com/manual/1.12/en/zend.controller.plugins.html. The postDispatch() method runs after your controller code but before the page is rendered.

0
votes

I just use:

public function init()
{
    if ($this->getHelper('FlashMessenger')->hasMessages()) {
        $this->view->messages = $this->getHelper('FlashMessenger')->getMessages();
    }
}

I use this in init() and it works fine. You could use it in postDispatch(), preDispatch() or dispatchLoopShutdown() if you like. A controller plugin would not be out of line, I just haven't gotten around to doing one yet.