Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

output in xml

Hello!!!

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



81.8k

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


22.5k

Thank you very much



22.5k

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



81.8k
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);        
            }
        }

    }

}


22.5k

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



81.8k

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