Other forms of export – RSS
Yii allows us to create a custom format response to output data. The response format can be changed based on the Accept
HTTP header sent by the client or done programmatically. When Yii receives a request, it searches for an available response formatter based on the Accept
HTTP header value and finally calls the format
($response
) method of the response formatter found.
Therefore, there are three steps to create custom responses:
- Implementing the
yii\web\ResponseFormatterInterface
interface. - Adding a new custom formatter response property in the configuration file.
- Extending the
behaviors()
method of the controller to handle specificAccept
HTTP header values.
The first step requires us to implement the yii\web\ResponseFormatterInterface
interface and extend its method format
($response
). Data to be formatted is stored in the $response->data
property, and the response to client must be filled out in the $response->content
property:
<?php namespace api...