我的控制器中有一个REST删除操作,它看起来像:
$asset = $this->Assets->get($id);
if ($this->Assets->delete($asset)) {
$this->response->statusCode(204);
}
在控制器中初始化我已完成
$this->RequestHandler->renderAs($this, 'json');
但出于某种原因,尽管是REST API,我收到错误"Template file \"Assets/json/delete.ctp\" is missing.",
蛋糕书建议只需要response->statusCode
,为什么要试图获取视图模板?
(v3)的
答案 0 :(得分:2)
设置状态代码本身就不够了;所有这一切都是,在响应对象上设置状态代码。然后代码仍将通过normal render process:
// Dispatcher code
$response = $controller->invokeAction();
...
if (!$response && $controller->autoRender) {
$response = $controller->render(); // Code execution reaches here
...
要让调度过程绕过普通视图渲染,请返回响应对象:
function foo()
{
$this->response->setStatusCode(204);
return $this->response;
}