I have tested code from budiirawan.com/setup-restful-api-yii2/, yes they were run correctly. But the problem is how to turn on json pretty print. Can you suggest me to enable pretty print? Thanks in advance.
I have tested code from budiirawan.com/setup-restful-api-yii2/, yes they were run correctly. But the problem is how to turn on json pretty print. Can you suggest me to enable pretty print? Thanks in advance.
After taking some test, the samples api running well with JSON_PRETTY_PRINT,
Here step :
Just download sample code from [font="Courier New"]github.com/deerawan/yii2-advanced-api[/font]
add response section in components, [font="Courier New"]api/config/main.php[/font]
'components' => [
...
'response' => [
//'format' => api\components\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
'formatters' => [
'json' => '\api\components\web\PrettyJsonResponseFormatter',
],
...
],
...
]
class PrettyJsonResponseFormatter extends \yii\web\JsonResponseFormatter
{
protected function formatJson($response)
{
$response->getHeaders()->set('Content-Type', 'application/json; charset=UTF-8');
if ($response->data !== null) {
$response->content = Json::encode($response->data, JSON_PRETTY_PRINT);
}
}
}
public function behaviors(){
$behaviors = parent::behaviors();
$behaviors['bootstrap'] = [
'class' => \yii\filters\ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
];
return $behaviors;
}
the pretty print output ready
6456