我知道可以设置 Yii::$app->response->format = \yii\web\Response::FORMAT_JSON
,但是如何设置JSON编码标志JSON_FORCE_OBJECT
,以便所有数组都将被编码为对象?
我知道可以设置 Yii::$app->response->format = \yii\web\Response::FORMAT_JSON
,但是如何设置JSON编码标志JSON_FORCE_OBJECT
,以便所有数组都将被编码为对象?
好的,这并不难:
Yii::$app->response->formatters[\yii\web\Response::FORMAT_JSON] = [
'class' => 'yii\web\JsonResponseFormatter',
'encodeOptions' => JSON_FORCE_OBJECT,
];
Yii::$app->response->formatters[\yii\web\Response::FORMAT_JSON]['encodeOptions'] = JSON_FORCE_OBJECT;