# Class yii\rest\Serializer

Inheritance | yii\rest\Serializer » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/rest/Serializer.php

Serializer converts resource objects and collections into array representation.

Serializer is mainly used by REST controllers to convert different objects into array representation so that they can be further turned into different formats, such as JSON, XML, by response formatters.

The default implementation handles resources as yii\base\Model objects and collections as objects implementing yii\data\DataProviderInterface. You may override serialize() to handle more types.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$collectionEnvelope | string | The name of the envelope (e.g. `items`) for returning the resource objects in a collection. | yii\rest\Serializer
$currentPageHeader | string | The name of the HTTP header containing the information about the current page number (1-based). | yii\rest\Serializer
$expandParam | string | The name of the query parameter containing the information about which fields should be returned in addition to those listed in $fieldsParam for a resource object. | yii\rest\Serializer
$fieldsParam | string | The name of the query parameter containing the information about which fields should be returned for a yii\base\Model object. | yii\rest\Serializer
$linksEnvelope | string | The name of the envelope (e.g. `_links`) for returning the links objects. | yii\rest\Serializer
$metaEnvelope | string | The name of the envelope (e.g. `_meta`) for returning the pagination object. | yii\rest\Serializer
$pageCountHeader | string | The name of the HTTP header containing the information about total number of pages of data. | yii\rest\Serializer
$perPageHeader | string | The name of the HTTP header containing the information about the number of data items in each page. | yii\rest\Serializer
$preserveKeys | boolean | Whether to preserve array keys when serializing collection data. | yii\rest\Serializer
$request | yii\web\Request | The current request. | yii\rest\Serializer
$response | yii\web\Response | The response to be sent. | yii\rest\Serializer
$totalCountHeader | string | The name of the HTTP header containing the information about total number of data items. | yii\rest\Serializer

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__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() | Serializes the given data into a format that can be easily turned into other formats. | yii\rest\Serializer
trigger() | Triggers an event. | yii\base\Component

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
addPaginationHeaders() | Adds HTTP headers about the pagination to the response. | yii\rest\Serializer
getRequestedFields() |  | yii\rest\Serializer
serializeDataProvider() | Serializes a data provider. | yii\rest\Serializer
serializeModel() | Serializes a model object. | yii\rest\Serializer
serializeModelErrors() | Serializes the validation errors in a model. | yii\rest\Serializer
serializeModels() | Serializes a set of models. | yii\rest\Serializer
serializePagination() | Serializes a pagination into an array. | yii\rest\Serializer

## Property Details

$collectionEnvelope public property

The name of the envelope (e.g. `items`) for returning the resource objects in a collection. This is used when serving a resource collection. When this is set and pagination is enabled, the serializer will return a collection in the following format:

    [
        'items' => [...],  // assuming collectionEnvelope is "items"
        '_links' => {  // pagination links as returned by Pagination::getLinks()
            'self' => '...',
            'next' => '...',
            'last' => '...',
        },
        '_meta' => {  // meta information as returned by Pagination::toArray()
            'totalCount' => 100,
            'pageCount' => 5,
            'currentPage' => 1,
            'perPage' => 20,
        },
    ]

If this property is not set, the resource arrays will be directly returned without using envelope. The pagination information as shown in `_links` and `_meta` can be accessed from the response HTTP headers.

public string $collectionEnvelope = null
$currentPageHeader public property

The name of the HTTP header containing the information about the current page number (1-based). This is used when serving a resource collection with pagination.

public string $currentPageHeader = 'X-Pagination-Current-Page'
$expandParam public property

The name of the query parameter containing the information about which fields should be returned in addition to those listed in $fieldsParam for a resource object.

public string $expandParam = 'expand'
$fieldsParam public property

The name of the query parameter containing the information about which fields should be returned for a yii\base\Model object. If the parameter is not provided or empty, the default set of fields as defined by yii\base\Model::fields() will be returned.

public string $fieldsParam = 'fields'
$linksEnvelope public property (available since version 2.0.4)

The name of the envelope (e.g. `_links`) for returning the links objects. It takes effect only, if `collectionEnvelope` is set.

public string $linksEnvelope = '_links'
$metaEnvelope public property (available since version 2.0.4)

The name of the envelope (e.g. `_meta`) for returning the pagination object. It takes effect only, if `collectionEnvelope` is set.

public string $metaEnvelope = '_meta'
$pageCountHeader public property

The name of the HTTP header containing the information about total number of pages of data. This is used when serving a resource collection with pagination.

public string $pageCountHeader = 'X-Pagination-Page-Count'
$perPageHeader public property

The name of the HTTP header containing the information about the number of data items in each page. This is used when serving a resource collection with pagination.

public string $perPageHeader = 'X-Pagination-Per-Page'
$preserveKeys public property (available since version 2.0.10)

Whether to preserve array keys when serializing collection data. Set this to `true` to allow serialization of a collection as a JSON object where array keys are used to index the model objects. The default is to serialize all collections as array, regardless of how the array is indexed.

See also serializeDataProvider().

public boolean $preserveKeys = false
$request public property

The current request. If not set, the `request` application component will be used.

public yii\web\Request $request = null
$response public property

The response to be sent. If not set, the `response` application component will be used.

public yii\web\Response $response = null
$totalCountHeader public property

The name of the HTTP header containing the information about total number of data items. This is used when serving a resource collection with pagination.

public string $totalCountHeader = 'X-Pagination-Total-Count'

## Method Details

addPaginationHeaders() protected method

Adds HTTP headers about the pagination to the response.

protected void addPaginationHeaders ( $pagination )
$pagination | yii\data\Pagination |

getRequestedFields() protected method

See also:

- yii\base\Model::fields()
- yii\base\Model::extraFields()

protected array getRequestedFields ( )
return | array | The names of the requested fields. The first element is an array representing the list of default fields requested, while the second element is an array of the extra fields requested in addition to the default fields.

init() public method

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 ( )

serialize() public method

Serializes the given data into a format that can be easily turned into other formats.

This method mainly converts the objects of recognized types into array representation. It will not do conversion for unknown object types or non-object data. The default implementation will handle yii\base\Model, yii\data\DataProviderInterface and \JsonSerializable. You may override this method to support more object types.

public mixed serialize ( $data )
$data | mixed | The data to be serialized.
return | mixed | The converted data.

serializeDataProvider() protected method

Serializes a data provider.

protected array serializeDataProvider ( $dataProvider )
$dataProvider | yii\data\DataProviderInterface |
return | array | The array representation of the data provider.

serializeModel() protected method

Serializes a model object.

protected array serializeModel ( $model )
$model | yii\base\Arrayable |
return | array | The array representation of the model

serializeModelErrors() protected method

Serializes the validation errors in a model.

protected array serializeModelErrors ( $model )
$model | yii\base\Model |
return | array | The array representation of the errors

serializeModels() protected method

Serializes a set of models.

protected array serializeModels ( array $models )
$models | array |
return | array | The array representation of the models

serializePagination() protected method

Serializes a pagination into an array.

See also addPaginationHeaders().

protected array serializePagination ( $pagination )
$pagination | yii\data\Pagination |
return | array | The array representation of the pagination