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?
有人能指出我正确的方向如何实现这一目标?
2 个解决方案
#1
9
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 DomDocument instead of typing out xml directly
此外,您可能希望使用DomDocument而不是直接键入xml
#2
25
UPDATE
UPDATE
Apparently, Zend Framework provides a way better method for that out of the box. Please do check the ContextSwitch action helper documentation.
显然,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);
}
}
#1
9
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 DomDocument instead of typing out xml directly
此外,您可能希望使用DomDocument而不是直接键入xml
#2
25
UPDATE
UPDATE
Apparently, Zend Framework provides a way better method for that out of the box. Please do check the ContextSwitch action helper documentation.
显然,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);
}
}