![]() |
Formatting REST response in JSON - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: Formatting REST response in JSON (/showthread.php?tid=77918) |
Formatting REST response in JSON - neilpopham - 11-04-2020 So, I'm trying to follow the docs here: https://codeigniter.com/user_guide/incoming/restful.html In my extended ResourceController I'm setting $format = 'json', but I always get XML. I've run through the code of ResourceController.php, ResponseTrait.php and Negotiate.php and I can see nowhere where this property is actually used to decide the response format! As it stands it seems like the headers are always used to determine the response. Can anyone confirm or explain where I may have messed up? Here's the relevant code: <?php namespace App\Controllers; use CodeIgniter\RESTful\ResourceController; class Salutation extends ResourceController { protected $modelName = 'App\Models\SalutationModel'; protected $format = 'json'; public function index() { // print '<pre>'; print_r($this->model->asArray()->findAll()); exit; // $this->setFormat("json"); return $this->respond($this->model->findAll()); } RE: Formatting REST response in JSON - neilpopham - 11-04-2020 Here's how I see the flow: app\Controllers\Salutation.php PHP Code: return $this->respond($this->model->findAll()); vendor\codeigniter4\framework\system\API\ResponseTrait.php PHP Code: public function respond($data = null, int $status = null, string $message = '') PHP Code: protected function format($data = null) vendor\codeigniter4\framework\system\HTTP\IncomingRequest.php PHP Code: public function negotiate(string $type, array $supported, bool $strictMatch = false): string vendor\codeigniter4\framework\system\HTTP\Negotiate.php PHP Code: public function media(array $supported, bool $strictMatch = false): string I think you get the picture, the property should have been used way before now. Perhaps ResourceController is supposed to override ResponseTrait's format() method and check for $format before using $this->request->negotiate? RE: Formatting REST response in JSON - neilpopham - 11-05-2020 Okay, I see this is a known and fixed bug. https://github.com/codeigniter4/CodeIgniter4/issues/2828 Sorry, I didn't know that GitHub was used for bugs. |