[solved] Pretty Print In JSON RESTfull Output ?

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 :

  1. Just download sample code from [font="Courier New"]github.com/deerawan/yii2-advanced-api[/font]

  2. 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',

			],

			...

		],

...

]

  1. extend class [font="Courier New"]\yii\web\JsonResponseFormatter[/font]



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);

        }

    }

}



  1. add behaviors in [font="Courier New"]modules\v1\controller\CountryController.php[/font]

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

prettyprint.png