需要您的帮助来生成.xml
文件)我需要将结果从 View 获取到我的变量 $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>
最佳答案
我们应该将 View 渲染级别设置为LEVEL_ACTION_VIEW
,内容仅从partial
输出,然后我们发送xml
header :)仅此而已)
//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>
关于php - 使用 Phalcon 生成 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444366/