I know I can set Yii::$app->response->format = \yii\web\Response::FORMAT_JSON but how I can make it set the JSON encoding flag JSON_FORCE_OBJECT so that all arrays will be encoded as objects instead?
-
1yiiframework.com/doc/api/2.0/yii-web-jsonresponseformatter04FS– 04FS2019-04-25 09:53:14 +00:00Commented Apr 25, 2019 at 9:53
Add a comment
|
1 Answer
Alright, it wasn't so hard:
Yii::$app->response->formatters[\yii\web\Response::FORMAT_JSON] = [
'class' => 'yii\web\JsonResponseFormatter',
'encodeOptions' => JSON_FORCE_OBJECT,
];
Or even as a one-liner:
Yii::$app->response->formatters[\yii\web\Response::FORMAT_JSON]['encodeOptions'] = JSON_FORCE_OBJECT;