2013-05-31 29 views

回答

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