phpzend-frameworkzend-routezend-rest

Change default REST response to be only XML in Zend


I've worked on this code base and it responds with html when i access a site www.site.com/version/

However, If i access www.site.com/version?format=xml, it displays output in xml.

How can I change the Zend code to ONLY output in XML irrespective of the format request? Yeah, i'm new to Zend coding...)

My code is exactly what Chris has ( http://www.chrisdanielson.com/2009/09/02/creating-a-php-rest-api-using-the-zend-framework/ ) :

class VersionController extends Zend_Rest_Controller
{  
public function init()
{
    $bootstrap = $this->getInvokeArg('bootstrap');

    $options = $bootstrap->getOption('resources');

    $contextSwitch = $this->_helper->getHelper('contextSwitch');
    $contextSwitch->addActionContext('index', array('xml','json'))->initContext();

    //$this->_helper->viewRenderer->setNeverRender();   
    $this->view->success = "true";
    $this->view->version = "1.0";
}
 ...
 ...

Solution

  • You can force the context to use XML only by following code:

    $this->_helper->contextSwitch()->initContext('xml');

    Refer link:

    http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch.initcontext