|
|
|
@ -86,8 +86,8 @@ public function behaviors()
|
|
|
|
|
|
|
|
|
|
正如我们上面所描述的,[[yii\rest\Serializer]] 负责转换资源的中间件 |
|
|
|
|
对象或集合到数组。它将对象 [[yii\base\ArrayableInterface]] 作为 |
|
|
|
|
[[yii\data\DataProviderInterface]]。 前者主要由资源对象实现, 而 |
|
|
|
|
后者是资源集合。 |
|
|
|
|
[[yii\data\DataProviderInterface]]。前者主要由资源对象实现, |
|
|
|
|
而后者是资源集合。 |
|
|
|
|
|
|
|
|
|
你可以通过设置 [[yii\rest\Controller::serializer]] 属性和一个配置数组。 |
|
|
|
|
例如,有时你可能想通过直接在响应主体内包含分页信息来 |
|
|
|
@ -107,7 +107,7 @@ class UserController extends ActiveController
|
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
那么你的请求可能会得到的响应如下 `http://localhost/users`: |
|
|
|
|
那么你的请求可能会得到的响应如下 `http://localhost/users`: |
|
|
|
|
|
|
|
|
|
``` |
|
|
|
|
HTTP/1.1 200 OK |
|
|
|
@ -156,16 +156,16 @@ Content-Type: application/json; charset=UTF-8
|
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
### Controlling JSON output |
|
|
|
|
### 控制 JSON 输出 |
|
|
|
|
|
|
|
|
|
The JSON response is generated by the [[yii\web\JsonResponseFormatter|JsonResponseFormatter]] class which will |
|
|
|
|
use the [[yii\helpers\Json|JSON helper]] internally. This formatter can be configured with different options like |
|
|
|
|
for example the [[yii\web\JsonResponseFormatter::$prettyPrint|$prettyPrint]] option, which is useful on development for |
|
|
|
|
better readable responses, or [[yii\web\JsonResponseFormatter::$encodeOptions|$encodeOptions]] to control the output |
|
|
|
|
of the JSON encoding. |
|
|
|
|
JSON 响应将由 [[yii\web\JsonResponseFormatter|JsonResponseFormatter]] 类来生成, |
|
|
|
|
并且将在内部使用 [[yii\helpers\Json|JSON helper]]。 |
|
|
|
|
这个格式化程序可以配置不同的选项, |
|
|
|
|
比如 [[yii\web\JsonResponseFormatter::$prettyPrint|$prettyPrint]],这对于开发更好的可读式响应更有用, |
|
|
|
|
或者用 [[yii\web\JsonResponseFormatter::$encodeOptions|$encodeOptions]] 去控制 JSON 编码的输出。 |
|
|
|
|
|
|
|
|
|
The formatter can be configured in the [[yii\web\Response::formatters|formatters]] property of the `response` application |
|
|
|
|
component in the application [configuration](concept-configuration.md) like the following: |
|
|
|
|
格式化程序可以在 [configuration](concept-configuration.md) 的 `response` 应用程序组件 [[yii\web\Response::formatters|formatters]] 的属性进行配置, |
|
|
|
|
如下所示: |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
'response' => [ |
|
|
|
@ -181,7 +181,7 @@ component in the application [configuration](concept-configuration.md) like the
|
|
|
|
|
], |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
When returning data from a database using the [DAO](db-dao.md) database layer all data will be represented |
|
|
|
|
as strings, which is not always the expected result especially numeric values should be represented as |
|
|
|
|
numbers in JSON. When using the ActiveRecord layer for retrieving data from the database, the values for numeric |
|
|
|
|
columns will be converted to integers when data is fetched from the database in [[yii\db\ActiveRecord::populateRecord()]]. |
|
|
|
|
当使用 [DAO](db-dao.md) 数据库层从数据库返回数据时,所有的数据将会表示成字符串, |
|
|
|
|
这不总是预期的结果,尤其是数值应该表现为 JSON 的中的数字时。 |
|
|
|
|
当使用 ActiveRecord 层从数据库检索数据时, |
|
|
|
|
在 [[yii\db\ActiveRecord::populateRecord()]] 中从数据库中提取数据,数字列的值将会被转换为整数。 |
|
|
|
|