Formatting REST response in JSON |
So, I'm trying to follow the docs here:
https://codeigniter.com/user_guide/incom...stful.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()); }
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?
Okay, I see this is a known and fixed bug.
https://github.com/codeigniter4/CodeIgni...ssues/2828 Sorry, I didn't know that GitHub was used for bugs. |
Welcome Guest, Not a member yet? Register Sign In |