How to define content-type with Code Igniter REST_Controller How to define content-type with Code Igniter REST_Controller codeigniter codeigniter

How to define content-type with Code Igniter REST_Controller


I'm not sure if the config sets the format. But a simple work around might be just to use the output class to set the header content type, something like:

$this->output    ->set_content_type('application/json')    ->set_output(json_encode(array('foo' => 'bar')));

(Taken from the manual: here)


While setting contect_type at each functions helps, this can be made generic at controller level by setting this at constructor.

public function __construct() {    parent::__construct();    ...    $this->output->set_content_type('application/json');}

So you would simply set the output at each function level

$this->output->set_output('{"message":"Failure"}');

This worked for me.