<?php
class ZendL_Controller_Action_Helper_ContextSwitch
extends Zend_Controller_Action_Helper_ContextSwitch
{
//adds jsonp support for json context
protected $_json_callback = false;
//can set up a xml serializer (maybe).
protected $_autoXmlSerialization = false;
public function init() {
parent::init();
$this->setContext('rss', array('suffix' => 'rss',
'headers' => array('Content-Type' => 'application/rss+xml',
'charset' => 'utf-8'),
))
->setContext('atom', array('suffix' => 'atom',
'headers' => array('Content-Type' => 'application/atom+xml',
'charset' => 'utf-8'),
))
->setContext('xml', array('suffix' => 'xml',
'headers' => array('Content-Type' => 'application/xml',
'charset' => 'utf-8'),
'callbacks' => array('init' => 'initXmlContext',
'post' => 'postXmlContext')
))
;
}
public function initJsonContext() {
$this->getRequest()->getParam('callback'));
if ($this->_json_callback) {
$this->getResponse()->setHeader('Content-Type', 'application/javascript');
}
return parent::initJsonContext();
}
public function postJsonContext() {
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (!$this->getAutoJsonSerialization()) {
//push the render now, since we don't know what order the
//action helpers are in, and we'll need it rendered already for postdispatch
$viewRenderer->render();
return;
}
return parent::postJsonContext();
}
public function postDispatch() {
parent::postDispatch();
if ($this->_json_callback) {
$body = $this->getResponse()->getBody();
$body = $this->_json_callback . '(' . $body . ');';
$this->getResponse()->setBody($body);
}
}
public function initXmlContext() {
if (!$this->getAutoXmlSerialization()) {
return;
}
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$view = $viewRenderer->view;
if ($view instanceof Zend_View_Interface) {
$viewRenderer->setNoRender(true);
}
}
public function setAutoXmlSerialization($flag) {
$this->_autoXmlSerialization = (bool) $flag;
return $this;
}
public function getAutoXmlSerialization() {
return $this->_autoXmlSerialization;
}
//todo allow customized serializer
public function postXmlContext() {
if (!$this->getAutoXmlSerialization()) {
return;
}
//this section assumes there is a viewhelper that renders xml from the vars in the default view.
//this is a todo to define the serializer here.
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$view = $viewRenderer->view;
if ($view instanceof Zend_View_Interface) {
$vars = $view->xmlfromview();
$this->getResponse()->setBody($vars);
} else {
// require_once 'Zend/Controller/Action/Exception.php';
throw new Zend_Controller_Action_Exception('View does not implement the getVars() method needed to encode the view into XML');
}
}
}
}
class HomeController extends Zend_Controller_Action {
public function init() {
parent::init();
$this->_helper->contextSwitch()
->setActionContext('index', array('rss', 'atom'))
->initContext();
//todo make feed contexts more pages or something
}
//..... actions happen below as usual
// anything in $view is available in actionname.atom.phtml and actionname.rss.phtml
// right alongside actionname.phtml
// the autoserialization stuff can be turned on too.
}