【问题标题】:Output all actions in Phalcon as XML将 Phalcon 中的所有操作输出为 XML
【发布时间】:2013-05-31 19:58:23
【问题描述】:

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

如何做到这一点?

【问题讨论】:

    标签: php xml phalcon


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2019-05-11
      • 2016-03-15
      • 2011-09-15
      相关资源
      最近更新 更多