メモ。
$this->response->type('json');
$this->response->send();
以上!
最後のsend()関数は通常リクエストの最後Dispatcherによって自動的に行われるが、自ら処理を殺している場合などはヘッダの送信処理が行われないため、自分で実行してあげないと想定した挙動にならないので注意。
例えば以下のような場合。
// 何かしらのデータ配列
$data = array('my' => 'apple', 'favorite' => 'banana', 'food' => 'orange');
$this->response->type('json');
$this->response->send();
// jsonデータとして出力して処理終了
echo json_encode($data);
die();
Ajaxの処理とかでjson形式のデータをコールバック関数に渡したい時などにやるやーつー。