views:

100

answers:

1

Hi there,

I'm trying to access the final xhtml output right before it's sent to the browser as a string. The postDispatch() methods of the actions and plugins seem to be too early to do this. When I step through Zend_Controller_Front::dispatch() method using a debugger, I can access the desired output as a string right before $this->_response->sendResponse() is called at the very end by adding a watch expression $this->getResponse()->getBody(). However, there seems to be no dedicated hook to tap into right there. I need the final response body as a string in order to send it to Prince XML to generate a pdf. Does anybody know an elegant way to do this?

Thanks, Adrian

+6  A: 

The Zend_Controller_Front plugin hooks are the following (from here):

  • routeStartup() is called before Zend_Controller_Front calls on the router to evaluate the request against the registered routes.
  • routeShutdown() is called after the router finishes routing the request.
  • dispatchLoopStartup() is called before Zend_Controller_Front enters its dispatch loop.
  • preDispatch() is called before an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via Zend_Controller_Request_Abstract::setDispatched(false)), the current action may be skipped and/or replaced.
  • postDispatch() is called after an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via Zend_Controller_Request_Abstract::setDispatched(false)), a new action may be specified for dispatching.
  • dispatchLoopShutdown() is called after Zend_Controller_Front exits its dispatch loop.

So dispatchLoopShutdown() is your hook to go - it's the last thing Zend_Controller_Front::dispatch() does before returning or sending the response.

Another option could be, even though they were designed for something completely different, to use Zend_View filters. These filters can be added to the Zend_View-instance and are called in Zend_View::render(). A filter is simply an instance of a class that provides a filter($buffer)-method that returns the filtered $buffer. But using this interface for something not related to filtering the ouptut, seems not to be the correct way actually.

I personally think, that a dispatchLoopShutdown()-plugin will be the way to go.

Stefan Gehrig
That was the perfect answer, thank you so much! I bow to the infinite wisdom of SO :)
Wunibald