在 Zend 中将默认 REST 响应更改为仅 XML
我已经研究过这个代码库,当我访问网站 www.site.com/version/ 时,它会以 html 进行响应,
但是,如果我访问 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";
}
...
...
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";
}
...
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过以下代码强制上下文仅使用 XML:
$this->_helper->contextSwitch()->initContext('xml');
参考链接:
http:// Framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch.initcontext
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