Class yii\rest\Serializer
| 继承 | yii\rest\Serializer » yii\base\Component » yii\base\BaseObject |
|---|---|
| 实现 | yii\base\Configurable |
| 可用版本自 | 2.0 |
| 源码 | https://github.com/yiichina/yii2/blob/api/framework/rest/Serializer.php |
Serializer 将资源对象和集合转换为数组表示。
Serializer 主要由 REST 控制器用于将不同的对象转换为数组表示 这样它们可以通过响应格式化程序进一步转换为不同的格式,例如JSON,XML。
默认的实现:将 yii\base\Model 对象作为资源处理,将 yii\data\DataProviderInterface 对象作为集合处理。 你可以覆盖 serialize() 来处理更多类型。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
| $collectionEnvelope | string | 用于返回集合中资源对象的索引名称(例如 items)。
在提供资源集合时使用。设置此选项并启用分页后
将以以下格式返回集合:
`php
[
'items' => [. |
yii\rest\Serializer |
| $currentPageHeader | string | HTTP 标头名称,包含有关数据项当前页数的信息。 在使用分页提供资源集合时使用此选项。 | yii\rest\Serializer |
| $expandParam | string | 查询参数的名称,包含有关应返回哪些字段的信息 除了资源对象的 $fieldsParam 中列出的之外。 | yii\rest\Serializer |
| $fieldsParam | string | 查询参数的名称,包含有关应返回哪些字段的信息 对于 yii\base\Model 对象,如果未提供参数或为空,则 yii\base\Model::fields() 定义的默认字段集 将被返回。 | yii\rest\Serializer |
| $linksEnvelope | string | 用于返回链接对象的索引名称(例如 _links)。
collectionEnvelope 设置后它才生效。 |
yii\rest\Serializer |
| $metaEnvelope | string | 用于返回链接对象的索引名称(例如 _meta)。
collectionEnvelope 设置后它才生效。 |
yii\rest\Serializer |
| $pageCountHeader | string | HTTP 标头名称,包含有关数据项总页数的信息。 在使用分页提供资源集合时使用此选项。 | yii\rest\Serializer |
| $perPageHeader | string | HTTP 标头名称,包含有关数据项每页的数据个数的信息 在使用分页提供资源集合时使用此选项。 | yii\rest\Serializer |
| $preserveKeys | boolean | 在序列化集合数据时是否保留数组键。
将其设置为 true 以允许将集合序列化为 JSON 对象时,数组键用于索引对应的模型对象。
无论如何,默认是将所有集合序列化为数组
如何索引数组。 |
yii\rest\Serializer |
| $request | yii\web\Request | 当前请求,未设置则默认应用中的 request 组件。 |
yii\rest\Serializer |
| $response | yii\web\Response | 当前响应,未设置则默认应用中的 request 组件。 |
yii\rest\Serializer |
| $totalCountHeader | string | HTTP 标头名称,包含有关数据项总数的信息。 在使用分页提供资源集合时使用此选项。 | yii\rest\Serializer |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\base\Component |
| __clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
| __construct() | Constructor. | yii\base\BaseObject |
| __get() | Returns the value of a component property. | yii\base\Component |
| __isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
| __set() | Sets the value of a component property. | yii\base\Component |
| __unset() | Sets a component property to be null. | yii\base\Component |
| attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
| attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
| behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
| canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
| canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
| className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
| detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
| detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
| ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
| getBehavior() | Returns the named behavior object. | yii\base\Component |
| getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
| hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
| hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
| hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
| init() | Initializes the object. | yii\rest\Serializer |
| off() | Detaches an existing event handler from this component. | yii\base\Component |
| on() | Attaches an event handler to an event. | yii\base\Component |
| serialize() | 将给定数据序列化为可轻松转换为其他格式的格式。 此方法主要将识别类型的对象转换为数组表示。 它不会对未知对象类型或非对象数据进行转换。 默认的实现是仅仅处理 yii\base\Model 和 yii\data\DataProviderInterface 。 你可以覆盖此方法以支持更多对象类型。 | yii\rest\Serializer |
| trigger() | Triggers an event. | yii\base\Component |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| addPaginationHeaders() | 将分页信息加到 HTTP 响应头部。 | yii\rest\Serializer |
| getRequestedFields() | yii\rest\Serializer | |
| serializeDataProvider() | 序列化一个数据提供器。 | yii\rest\Serializer |
| serializeModel() | 序列化模型对象。 | yii\rest\Serializer |
| serializeModelErrors() | 序列化模型中的验证错误信息。 | yii\rest\Serializer |
| serializeModels() | 序列化一组模型 | yii\rest\Serializer |
| serializePagination() | 序列化分页器为数组格式。 | yii\rest\Serializer |
属性详情
用于返回集合中资源对象的索引名称(例如 items)。
在提供资源集合时使用。设置此选项并启用分页后
将以以下格式返回集合:
[
'items' => [...], // 假如设的索引是 "items"
'_links' => { // 分页链接集,此数据通过 Pagination::getLinks() 处理返回
'self' => '...',
'next' => '...',
'last' => '...',
},
'_meta' => { // Pagination::toArray() 返回的 meta 信息。
'totalCount' => 100,
'pageCount' => 5,
'currentPage' => 1,
'perPage' => 20,
},
]
如果未设置此属性,则将直接返回资源数组,而不使用索引。
_links 和 _meta 中显示的分页信息被放置到 HTTP 头部以供访问。
HTTP 标头名称,包含有关数据项当前页数的信息。 在使用分页提供资源集合时使用此选项。
查询参数的名称,包含有关应返回哪些字段的信息 除了资源对象的 $fieldsParam 中列出的之外。
查询参数的名称,包含有关应返回哪些字段的信息 对于 yii\base\Model 对象,如果未提供参数或为空,则 yii\base\Model::fields() 定义的默认字段集 将被返回。
用于返回链接对象的索引名称(例如 _links)。
collectionEnvelope 设置后它才生效。
用于返回链接对象的索引名称(例如 _meta)。
collectionEnvelope 设置后它才生效。
HTTP 标头名称,包含有关数据项总页数的信息。 在使用分页提供资源集合时使用此选项。
HTTP 标头名称,包含有关数据项每页的数据个数的信息 在使用分页提供资源集合时使用此选项。
在序列化集合数据时是否保留数组键。
将其设置为 true 以允许将集合序列化为 JSON 对象时,数组键用于索引对应的模型对象。
无论如何,默认是将所有集合序列化为数组
如何索引数组。
当前请求,未设置则默认应用中的 request 组件。
当前响应,未设置则默认应用中的 request 组件。
HTTP 标头名称,包含有关数据项总数的信息。 在使用分页提供资源集合时使用此选项。
方法详情
将分页信息加到 HTTP 响应头部。
| protected void addPaginationHeaders($pagination) | ||
| $pagination | yii\data\Pagination | |
| protected array getRequestedFields() | ||
| return | array | 所请求字段的名称。 The first element is an array 第一个元素是一个数组,表示请求的默认字段列表, 而第二个元素是除默认字段外,还请求的一系列额外字段列表。 |
|---|---|---|
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
| public void init() |
将给定数据序列化为可轻松转换为其他格式的格式。 此方法主要将识别类型的对象转换为数组表示。 它不会对未知对象类型或非对象数据进行转换。 默认的实现是仅仅处理 yii\base\Model 和 yii\data\DataProviderInterface 。 你可以覆盖此方法以支持更多对象类型。
| public mixed serialize($data) | ||
| $data | mixed | 要被序列化的数据。 |
| return | mixed | The 转换后的数据。 |
|---|---|---|
序列化一个数据提供器。
| protected array serializeDataProvider($dataProvider) | ||
| $dataProvider | yii\data\DataProviderInterface | |
| return | array | 数据提供器的数组表示。 |
|---|---|---|
序列化模型对象。
| protected array serializeModel($model) | ||
| $model | yii\base\Arrayable | |
| return | array | 模型对象的数组表示 |
|---|---|---|
序列化模型中的验证错误信息。
| protected array serializeModelErrors($model) | ||
| $model | yii\base\Model | |
| return | array | 验证错误的数组表示 |
|---|---|---|
序列化一组模型
| protected array serializeModels(array $models) | ||
| $models | array | |
| return | array | 一组模型的数组表示 |
|---|---|---|
序列化分页器为数组格式。
| protected array serializePagination($pagination) | ||
| $pagination | yii\data\Pagination | |
| return | array | 分页器的数组表示 |
|---|---|---|