【发布时间】:2021-07-29 07:44:38
【问题描述】:
我尝试在 CakePHP 中设置一个 Json 响应视图。 我多次阅读文档https://book.cakephp.org/4/en/views/json-and-xml-views.html,但找不到返回特定响应代码的解决方案。
这就是我目前的代码
$customers = $this->Customers->find('all')->where(['organisation_id' =>1] )->contain($this->contain)->toArray();
$this->set('customers', $customers);
$this->viewBuilder()->setOption('serialize', 'customers');
$this->viewBuilder()->setClassName('Json');
我知道我可以用这个返回一个 json 和一个响应代码
return $this->response->withStatus(400)->withType('json')->withStringBody(json_encode($customers));
但是使用此代码,您无法为其编写测试。我希望有人对此有解决方案。
【问题讨论】:
标签: cakephp cakephp-4.x