使用Phalcon生成XML

时间:2015-10-30 20:24:49

标签: php xml export 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>

1 个答案:

答案 0 :(得分:2)

我们应该将视图呈现级别设置为LEVEL_ACTION_VIEW,内容只是来自partial的输出,然后我们发送标题xml :)这就是全部的<) / p>

//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>