如何在 Zend Framework 应用程序中返回 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1542979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:47:35  来源:igfitidea点击:

How to return XML in a Zend Framework application

xmlzend-frameworkresponsereturn

提问by Jayawi Perera

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

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

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

I've also tried the following:

我还尝试了以下方法:

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?

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

采纳答案by Mark

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

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

<?xml version='1.0'>

It should be

它应该是

<?xml version='1.0'?>

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

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

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

You may want to consider the contextSwitch action helper

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

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

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

回答by

UPDATE

更新

Apparently, Zend Framework provides a way better method for that out of the box. Please do check the ContextSwitch action helperdocumentation.

显然,Zend Framework 提供了一种开箱即用的更好方法。请检查ContextSwitch 操作帮助器文档。

The only thing you might want to change is force XML context in controller's init() method.

您可能想要更改的唯一一件事是在控制器的 init() 方法中强制使用 XML 上下文。

<?php

class ProjectsController extends Gid_Controller_Action
{
    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('xml', 'xml')->initContext('xml');
    }

    public function xmlAction()
    {
    }
}



Old answer.旧答案。

It doesn't work because ZF renders both layout and template after your code.

它不起作用,因为 ZF 在您的代码之后呈现布局和模板。

I agree with Mark, layout should be disabled, though in addition you should also disable view renderer. And definitely DOMDocument is much more preferable when you're going to deal with XML.

我同意 Mark 的观点,应该禁用布局,但此外您还应该禁用视图渲染器。当您要处理 XML 时,肯定 DOMDocument 更可取。

Here is a sample controller that should do what you want:

这是一个示例控制器,应该可以执行您想要的操作:

<?php

class ProjectsController extends Gid_Controller_Action
{
    public function xmlAction()
    {
        // XML-related routine
        $xml = new DOMDocument('1.0', 'utf-8');
        $xml->appendChild($xml->createElement('foo', 'bar'));
        $output = $xml->saveXML();

        // Both layout and view renderer should be disabled
        Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
        Zend_Layout::getMvcInstance()->disableLayout();

        // Set up headers and body
        $this->_response->setHeader('Content-Type', 'text/xml; charset=utf-8')
            ->setBody($output);
    }
}