CakePHP4 の AppController でJSON形式で出力する _renderJson というメソッドを定義
class AppController extends Controller
{
(略)
protected function _renderJson( $inOutput , $force = false) {
// JSON で出力
$this->set('output', $inOutput);
$this->viewBuilder()
->setClassName('Json')
->setOption('serialize', ['output']);
if($force){
$this->viewBuilder()->setOption('jsonOptions', JSON_FORCE_OBJECT);
}
}
}
呼び出しは
$students = ['students' => ['hoge','ふが','fuga']]; $this->_renderJson($students);
出力結果
{
"output": {
"students": [
"hoge",
"\u3075\u304c",
"fuga"
]
}
}
第二パラメータをtrueにすると
$this->_renderJson($students, true);
{
"output": {
"students": {
"0": "hoge",
"1": "\u3075\u304c",
"2": "fuga"
}
}
}
東京造形大学卒業後、マクロメディア(現アドビ)に入社。QAやテクニカルサポートマネージャーとしてFlash、DreamweaverなどのWeb製品を担当。独立後、2007年に虫カゴデザインスタジオ株式会社を設立。2021年東京三鷹を拠点に。最近は、Unity, Unity Netcode for GameObjects, CakePHP, Laravel, ZBrush, Modo, Adobe Substance 3D, Adobe Firefly, Xcode, Apple Vision Pro, Firebaseにフォーカスしています。モバイルアプリ開発情報を主としたブログ「MUSHIKAGO APPS MEMO」の中の人。