Denys Klymenko
Denys Klymenko

Reputation: 423

Generate XML with Phalcon

Need your help to generate .xml file with Phalcon) I need to get the result from view into my variable $xmlContent. In output file there is only <?xml version="1.0"?> Here is my drafts. Thank You

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

Upvotes: 2

Views: 1302

Answers (1)

Denys Klymenko
Denys Klymenko

Reputation: 423

We should set view rendering level to LEVEL_ACTION_VIEW, content just outputs from partial and then we send header that's xml :) That's all)

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

Upvotes: 2

Related Questions