如何在 Zend Framework 应用程序中返回 XML [英] How to return XML in a Zend Framework application

查看:47
本文介绍了如何在 Zend Framework 应用程序中返回 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ZF 应用程序中返回 XML 时遇到问题.我的代码:

I'm having problems returning XML in my ZF application. My code:

class ProjectsController extends Gid_Controller_Action
{
    public function xmlAction ()
    {
        $content = "<?xml version='1.0'><foo>bar</foo>";
        header('Content-Type: text/xml');
        echo $content;
    }
}

我还尝试了以下方法:

class ProjectsController extends Gid_Controller_Action
{
    public function xmlAction ()
    {
        $content = "<?xml version='1.0'><foo>bar</foo>";
        $this->getResponse()->clearHeaders();
        $this->getResponse()->setheader('Content-Type', 'text/xml');
        $this->getResponse()->setBody($content);
        $this->getResponse()->sendResponse();
    }
}

有人能指出我如何实现这一目标的正确方向吗?

Could someone point me in the right direction how to achieve this?

推荐答案

您缺少 xml 标签上的结尾问号:

You're missing the ending question mark on the xml tag:

<?xml version='1.0'>

应该是

<?xml version='1.0'?>

此外,您可能需要禁用布局,以便它仅打印 xml.将此行放在您的 xmlAction() 方法中

Additionally, you will probably need to disable your layout so it prints only the xml. Put this line in your xmlAction() method

$this->_helper->layout->disableLayout();

您可能需要考虑 contextSwitch 操作助手

此外,您可能希望使用 DomDocument 而不是输入xml直接

Also, you may want to use DomDocument instead of typing out xml directly

这篇关于如何在 Zend Framework 应用程序中返回 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆