将Phalcon中的所有操作输出为XML

时间:2013-05-31 19:58:24

标签: php xml phalcon

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

如何实现这一目标?

1 个答案:

答案 0 :(得分: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";
    }
    
  2. 插件:

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