It is used by Response to format response data. To configure properties like [[encodeOptions]] or [[prettyPrint]], you can configure the response application component like the following: php 'response' => [ ... 'formatters' => [ \yii\web\Response::FORMAT_JSON => [ 'class' => 'yii\web\JsonResponseFormatter', 'prettyPrint' => YII_DEBUG, // use "pretty" output in debug mode ... ], ], ],
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Component, implements yii\web\ResponseFormatterInterface
 /**
  * Show export form or generate export file on post
  * @return string
  */
 public function run()
 {
     /** @var Module $module */
     $module = $this->controller->module;
     $model = new ExportForm(['format' => $module->defaultExportFormat]);
     if ($model->load(Yii::$app->request->post())) {
         $fileName = Yii::t('language', 'translations') . '.' . $model->format;
         Yii::$app->response->format = $model->format;
         Yii::$app->response->formatters = [Response::FORMAT_XML => ['class' => XmlResponseFormatter::className(), 'rootTag' => 'translations'], Response::FORMAT_JSON => ['class' => JsonResponseFormatter::className()]];
         Yii::$app->response->setDownloadHeaders($fileName);
         return $model->getExportData();
     } else {
         if (empty($model->languages)) {
             $model->exportLanguages = $model->getDefaultExportLanguages($module->defaultExportStatus);
         }
         return $this->controller->render('export', ['model' => $model]);
     }
 }
 public function format($response)
 {
     //Resulting data
     $resultData = ['code' => $response->getStatusCode(), 'result' => [], 'status' => $response->getIsSuccessful() ? 'success' : 'error'];
     if (!$response->getIsOk()) {
         $resultData['message'] = $response->statusText;
     }
     if (is_string($response->data)) {
         //For string result we send it like 'message'
         $resultData['message'] = $response->data;
     } elseif ($response->getIsClientError() && isset($response->data['message'])) {
         //For HttpExceptions we save message field only to 'message'
         $resultData['message'] = $response->data['message'];
         unset($response->data['message']);
         $resultData['result'] = $response->data;
     } else {
         //Otherwise send all as result
         $resultData['result'] = $response->data;
     }
     //Set resulting data to response->data and run parent format function
     $response->data = $resultData;
     parent::format($response);
 }
 /**
  * @inheritdoc
  */
 public function format($response)
 {
     $response->data = $this->serializeData($response->data);
     parent::format($response);
 }
 protected function formatJson($response)
 {
     parent::formatJson($response);
     $response->getHeaders()->set('Content-Type', $this->getContentType());
 }