yii\web\JsonResponseFormatter::format PHP Method

format() public method

Formats the specified response.
public format ( Response $response )
$response Response the response to be formatted.
    public function format($response)
    {
        if ($this->useJsonp) {
            $this->formatJsonp($response);
        } else {
            $this->formatJson($response);
        }
    }

Usage Example

 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);
 }
All Usage Examples Of yii\web\JsonResponseFormatter::format