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.

7
  • Not sure if the answer on XML creation using CodeIgniter helps, if so this can be closed as a duplicate. Commented Jul 19, 2020 at 6: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. Commented Jul 19, 2020 at 6: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. Commented Jul 19, 2020 at 6: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 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? Commented Jul 19, 2020 at 7:02
  • where to write "$this->output.."? Commented Jul 19, 2020 at 7:06

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.