Hello all, bit of a problem, not sure how to resolve it.

We've developed a RESTful JSON API using Zend Framework MVC. The API
makes heavy use of the Zend_View_Helper_Json object, each controller,
which represents a separate resource does the following when its done
processing:

$this->_helper->json( $data );

All of this was fine and dandy, until it was decided that we need
logging to record every request/response to the API. I setup a
BaseController which logs the request during preDispatch. Now, my
problem is that directly calling $this->_helper->json( $data ) sends
the response and exits, so, postDispatch never occurs. I've tried to
resolve this by extending Zend_View_Helper_Json as follows:

class BaseHelperJson extends Zend_View_Helper_Json
{
  public function json( $data, $keepLayouts = false )
  {
    // ... Do some logging ...
    parent::json( $data, $keepLayouts );
  }
}

Then during init() I tried replacing the Zend Json Helper with my own...

public function init()
{
  // Do some init...

  $this->_helper->removeHelper('json');
  $this->_helper->addHelper(new BaseHelperJson());

  // Whoops, how do I reference BaseHelperJson??
}

This causes an error because addHelper() expects
Zend_View_Helper_Abstract... regardless, I wouldn't know how to
reference my custom helper as $this->_helper->json anyway. So my
question: is there any way to resolve this using OOP or Zend MVC magic
or will I have to go through each controller and replace
$this->_helper->json with something that plays nice with the
dispatcher?

- jake

Reply via email to