I am just trying to play with the new Codeiginiter4 framework. I have created one route & attached it to one simple method todos which is supposed to return a list of todos(task) as JSON when I try to hit the URL but it returns it as XML format.
The code is simple,
class Home extends BaseController
{
use ResponseTrait;
public function index()
{
return view('welcome_message');
}
public function todos()
{
return $this->respondCreated(['todos' => ['task' => 'Check out new CI4']]);
}
//--------------------------------------------------------------------
}
// Result
<response>
<todos>
<task>Check out new CI4</task>
</todos>
</response>
Later I discovered that if I explicitly encode the array as JSON(using json_encode) it returns the result as JSON in the browser. Like this,
public function todos()
{
return $this->respondCreated(json_encode(['todos' => ['task' => 'Check out new CI4']]));
}
So my question is there a way to by default return array as JSON format in the browser in CI4?
CI Version I am using: v4.0.2