0

I'm a beginning codeigniter user, and I cannot find well readable examples nowhere. Here's a simple code in a controller

namespace App\Controllers;
class Home extends BaseController {
    public function index() {
        $xml = '<?xml version="1.0" encoding="UTF-8"?>
        <note><to>Tove</to><from>Jani</from><heading>Reminder</heading></note>';

        //$this->output->set_content_type('text/xml'); causes an error
        header("Content-type: text/xml");
        echo $xml;
        exit; //works only with the "exit".
    }
}

if I remove the "exit" or write "return $xml" after header(.., then the codeigniter removes somewhere the header ("Content-type: text/xml") and browser displays a plain text instead of correct xml-application.

Vickel
  • 7,879
  • 6
  • 35
  • 56
user2301515
  • 4,903
  • 6
  • 30
  • 46
  • Not sure if the answer on [XML creation using CodeIgniter](https://stackoverflow.com/questions/10361074/xml-creation-using-codeigniter) helps, if so this can be closed as a duplicate. – Nigel Ren Jul 19 '20 at 06:43
  • As a separate point I would recommend not creating XML as text, this can lead to errors in the XML which may be difficult to find. Something like SimpleXML could easily do this without too much complication. – Nigel Ren Jul 19 '20 at 06:44
  • I don't believe a "duplication" the codelines $this->load->dbutil(); $this->output->set_content_type(); $this->output->set_output(); every gives error messages. Everybody gives sample codes without well readable explanation where to use it and what to do that these works. – user2301515 Jul 19 '20 at 06:52
  • Sorry - I can't control the quality of other answers. I would have just closed the question if I was sure. Other answers like https://stackoverflow.com/a/53317972/1213708 seem to use a similar method. If you try it without the `$this->load->dbutil();` line, what errors do you get? – Nigel Ren Jul 19 '20 at 07:02
  • where to write "$this->output.."? – user2301515 Jul 19 '20 at 07:06
  • In your code, you show a line which you say shows an error - what is the error? – Nigel Ren Jul 19 '20 at 07:07
  • I found a solution https://codeigniter.com/user_guide/concepts/http.html . – user2301515 Jul 19 '20 at 07:15

1 Answers1

0

most probably after 1y you don't need this anymore, but just for future readers. Here's the piece of code that made it work for me:

    header("Content-type: text/xml");
    header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header("Content-Disposition: attachment; filename=file.xml;");
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    echo $xml;
Anwar S.
  • 116
  • 9