We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

output in xml

Hello!!!

How to display content in xml files??? This requires a custom template engine???



98.9k

Hi,

Printing just the XML:

public function showAsXmlAction()
{

    $response = new Phalcon\Http\Response();    

    $response->setHeader('Content-Type', 'application/xml');

    $response->setContent('<root><element><key>a</key><value>b</value></element></root>');

    return $response;
}

From a DOMDocument:

public function showAsXmlAction()
{
    $response = new Phalcon\Http\Response();

    $doc = new DOMDocument();   
    $doc->loadXML('<root><element><key>a</key><value>b</value></element></root>');

    $response->setHeader('Content-Type', 'application/xml');

    $response->setContent($doc->saveXML());

    return $response;
}


42.1k

Thank you very much



42.1k

and how to display everything in xml. so that all actions are taken out of xml



98.9k
Accepted
answer

There are several ways to accomplish that:

  1. Implement an afterExecuteRoute event
class MyController extends Phalcon\Mvc\Controller
{
    public function showAsXml1Action()
    {
        return "<root><key>k</key></root";
    }

    public function showAsXml2Action()
    {
        return "<root><key>k</key></root";
    }

    public function afterExecuteRoute($dispatcher)
    {
        $response = new Phalcon\Http\Response();
        $response->setHeader('Content-Type', 'application/xml');
        $response->setContent($dispatcher->getReturnedValue());
        $dispatcher->setReturnedValue($response);
    }
}
  1. Create a class that handles XML responses:
class MyXMLResponse extends Phalcon\Http\Response
{
    public function __construct($xml)
    {
        $this->setHeader('Content-Type', 'application/xml');
        $this->setContent($xml);
    }
}

in controller:

public function showAsXml2Action()
{
    return MyXmlResponse("<root><key>k</key></root");
}
  1. Create a plugin that handles XML responses:
class MyController extends Phalcon\Mvc\Controller
{
    public function showAsXml1Action()
    {
        return array(
            "type" => "xml",
            "content" => "<root><key>k</key></root"
        );
    }   

}

Plugin:

<?php

class ContentTypePlugin extends \Phalcon\Mvc\User\Plugin
{

    public function afterExecuteRoute($event, $dispatcher)
    {
        $content = $dispatcher->getReturnedValue();
        switch ($content['type']) {
            case 'xml':
                $response = new Phalcon\Http\Response();
                $response->setHeader('Content-Type', 'application/xml');
                $response->setContent($content['content']);
                $dispatcher->setReturnedValue($response);       
                break;
        }
    }

}
  1. Using annotations
/**
 * @Response(type="xml")
 */
public function showAction()
{
    return "<root><key>k</key></root";
}

Plugin:

<?php

class AnnotationsContentPlugin extends \Phalcon\Mvc\User\Plugin
{

    public function afterExecuteRoute($event, $dispatcher)
    {

        $annotations = $this->annotations->getMethod(
            $dispatcher->getActiveController(),
            $dispatcher->getActiveMethod()
        );

        //Check if the method has an annotation 'Response'
        if ($annotations->has('Response')) {

            //Get the type
            $type = $annotations->get('Response')->getNamedParameter('type');

            if ($type == 'xml') {
                $response = new Phalcon\Http\Response();
                $response->setHeader('Content-Type', 'application/xml');
                $response->setContent($dispatcher->getReturnedValue());
                $dispatcher->setReturnedValue($response);       
            }
        }

    }

}


42.1k

In this case, we cannot use the file hierarchy, for the xml output?

For example: app/views/index.phtml app/views/layouts/posts.phtml app/views/posts/show.phtml



98.9k

Yes, you can also use the view hierarchy:

public function showAction()
{
    $this->view->setRenderLevel(Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
    $this->view->xml = '<root><key>k</key></root>';
}

In the view:

<?php echo $xml; ?>