13

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;
    }
}

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?

halfpastfour.am
  • 5,764
  • 3
  • 44
  • 61
Jayawi Perera
  • 891
  • 1
  • 8
  • 17

2 Answers2

25

UPDATE

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

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

<?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.

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.

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);
    }
}
  • where do i place a file like this in the zend framework project folder structure? doesn't fit the MVC bucket paradigm – b_dubb Nov 03 '11 at 17:25
  • @b_dubb, you can generate XML in a view template, so it will be pretty much MVC. Or, you can wrap parts of the code in a helper. –  Nov 06 '11 at 12:51
  • @b_dubb, you can also check the ContextSwitch action helper: http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch –  Nov 06 '11 at 12:54
  • I updated answer, the ContextSwitch action helper is the proper way of doing this. –  Nov 06 '11 at 13:48
10

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

<?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

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

You may want to consider the contextSwitch action helper

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

Mark
  • 6,254
  • 1
  • 32
  • 31