0

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();
3
  • What do you mean by auto-refresh the flashMessenger? It's always fresh! Commented Jan 11, 2013 at 23:41
  • I mean to re-assigned the result of getMessages to the view->messages Commented Jan 11, 2013 at 23:46
  • You shouldn't need to do that, instead use a view helper to display the flash messages. Commented Jan 11, 2013 at 23:47

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I had tried postDispatch before reading all the answers here and it seems to be working properly. I wanted confirmation that it ran when I believed it did, thanks!
This isn't working for me. The view is already rendered by the time my code runs in postDispatch() assigning variables to view.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.