将默认REST响应更改为Zend中的XML

时间:2011-03-22 12:38:13

标签: php zend-framework zend-route zend-rest

我已经使用过此代码库,当我访问网站时,它会以html响应www.site.com/version /

但是,如果我访问www.site.com/version?format=xml,它会以xml显示输出。

无论格式请求如何,如何将Zend代码更改为仅以XML格式输出?是的,我是Zend编码的新手......)

我的代码正是Chris所拥有的(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";
}
 ...
 ...

1 个答案:

答案 0 :(得分:1)

您可以通过以下代码强制上下文使用XML:

$这 - > _helper-> ContextSwitch里() - >调用initContext( 'XML');

参考链接:

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