PHP Class yii\web\JsonResponseFormatter

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 ([email protected])
Inheritance: extends yii\base\Component, implements yii\web\ResponseFormatterInterface
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$encodeOptions the encoding options passed to [[Json::encode()]]. For more details please refer to . Default is JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE. This property has no effect, when [[useJsonp]] is true.
$prettyPrint whether to format the output in a readable "pretty" format. This can be useful for debugging purpose. If this is true, JSON_PRETTY_PRINT will be added to [[encodeOptions]]. Defaults to false. This property has no effect, when [[useJsonp]] is true.
$useJsonp whether to use JSONP response format. When this is true, the [[Response::data|response data]] must be an array consisting of data and callback members. The latter should be a JavaScript function name while the former will be passed to this function as a parameter.

Public Methods

Method Description
format ( Response $response ) Formats the specified response.

Protected Methods

Method Description
formatJson ( Response $response ) Formats response data in JSON format.
formatJsonp ( Response $response ) Formats response data in JSONP format.

Method Details

format() public method

Formats the specified response.
public format ( Response $response )
$response Response the response to be formatted.

formatJson() protected method

Formats response data in JSON format.
protected formatJson ( Response $response )
$response Response

formatJsonp() protected method

Formats response data in JSONP format.
protected formatJsonp ( Response $response )
$response Response

Property Details

$encodeOptions public property

the encoding options passed to [[Json::encode()]]. For more details please refer to . Default is JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE. This property has no effect, when [[useJsonp]] is true.
Since: 2.0.7
public $encodeOptions

$prettyPrint public property

whether to format the output in a readable "pretty" format. This can be useful for debugging purpose. If this is true, JSON_PRETTY_PRINT will be added to [[encodeOptions]]. Defaults to false. This property has no effect, when [[useJsonp]] is true.
Since: 2.0.7
public $prettyPrint

$useJsonp public property

whether to use JSONP response format. When this is true, the [[Response::data|response data]] must be an array consisting of data and callback members. The latter should be a JavaScript function name while the former will be passed to this function as a parameter.
public $useJsonp