0

我正在创建一个应用程序,我需要框架的所有操作来将数据输出为 XML。

如何做到这一点?

4

1 回答 1

2

有几种方法可以做到这一点:

1 实现一个afterExecuteRoute事件

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

2 创建一个处理 XML 响应的类:

    class MyXMLResponse extends Phalcon\Http\Response
    {
        public function __construct($xml)
        {
            $this->setHeader('Content-Type', 'application/xml');
            $this->setContent($xml);
        }
    }

在控制器中:

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

3 创建一个处理 XML 响应的插件:

    class MyController extends Phalcon\Mvc\Controller
    {
        public function showAsXml1Action()
        {
            return array(
                "type"    => "xml",
                "content" => "<root><key>k</key></root",
            );
        } 
    }

插入:

    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. 使用注释

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

插入:

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

如果您需要将文件层次结构用于 XML 输出,例如:

app/views/index.phtml
app/views/layouts/posts.phtml
app/views/posts/show.phtml

您可以在操作中使用以下代码

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

在视图中:

<?php echo $xml; ?>
于 2013-05-31T20:14:07.967 回答