使用 Phalcon 生成 XML

Generate XML with Phalcon

需要您的帮助以使用 Phalcon 生成 .xml 文件)我需要从视图中获取结果到我的变量 $xmlContent 中。在输出文件中只有 <?xml version="1.0"?> 这是我的草稿。谢谢

//in controller
public function exportAction(){

  $xmlContent = $this->view->partial('partial/xml/map', [
        'tks' => Tks::find()
     ]
  );

  $doc = new DOMDocument();
  $doc->loadXML($xmlContent);

  $this->response->setHeader('Content-Type', 'application/xml');
  $this->response->setHeader('Content-Disposition', 'attachment; filename="map.xml"');
  $this->response->setContent($doc->saveXML());

  return $this->response;
}

//in view
<tks>
{% for tk in tks %}
<tk>
  <type>{{ tk.tip_tk }}</type>
  <number>{{ tk.nomer  }}</number>
  <serial>{{ tk.seriyniy_nomer  }}</serial>
  <status>{{ tk.status_tk  }}</status>
</tk>
{% endfor %}
</tks>

我们应该将视图渲染级别设置为 LEVEL_ACTION_VIEW,内容只是从 partial 输出,然后我们发送 header 那就是 xml :) 就是这样)

//in controller
public function exportAction(){
   $this->view->setRenderLevel(Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
   $this->view->partial('partial/xml/map', [
     'tks' => Tks::find()
    ]
   );

   $this->response->setHeader('Content-Type', 'application/xml');
   $this->response->setHeader('Content-Disposition', 'attachment; filename="map.xml"');
}

//in view
{{ '<?xml version="1.0"?>' }}

<tks>
{% for tk in tks %}
  <tk>
     <type>{{ tk.tip_tk }}</type>
     <number>{{ tk.nomer }}</number>
     <serial>{{ tk.seriyniy_nomer }}</serial>
     <status>{{ tk.status_tk }}</status>
  </tk>
{% endfor %}
</tks>