如何在 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