# レスポンス形式の設定

> RESTful API のリクエストを処理するとき、アプリケーションは、通常、レスポンス形式の設定に関して次のステップを踏みます。

RESTful API のリクエストを処理するとき、アプリケーションは、通常、レスポンス形式の設定に関して次のステップを踏みます。

1. レスポンス形式に影響しうるさまざまな要因、例えば、メディア・タイプ、言語、バージョンなどを決定します。
   このプロセスは [コンテント・ネゴシエーション](https://en.wikipedia.org/wiki/Content_negotiation) としても知られるものです。
2. リソース・オブジェクトを配列に変換します。
   [リソース](/docs/ja/guide/rest-resources/index.md) のセクションで説明したように、この作業は [`Serializer`](/docs/api/yii/rest/serializer/index.md) によって実行されます。
3. 配列をコンテント・ネゴシエーションのステップで決定された形式の文字列に変換します。
   この作業は、`response` [アプリケーション・コンポーネント](/docs/ja/guide/structure-application-components/index.md) の [formatters](/docs/api/yii/web/response/index.md#property-formatters) プロパティに登録された [レスポンス・フォーマッタ](/docs/api/yii/web/responseformatterinterface/index.md) によって実行されます。

## コンテント・ネゴシエーション <span id="content-negotiation"></span>

Yii は [`ContentNegotiator`](/docs/api/yii/filters/contentnegotiator/index.md) フィルタによってコンテント・ネゴシエーションをサポートします。
RESTful API の基底コントローラ・クラス [`Controller`](/docs/api/yii/rest/controller/index.md) は `contentNegotiator` という名前でこのフィルタを持っています。
このフィルタは、レスポンス形式のネゴシエーションと同時に言語のネゴシエーションも提供します。
例えば、RESTful API リクエストが下記のヘッダを含んでいるとします。

```
Accept: application/json; q=1.0, */*; q=0.1
```

この場合、リクエストは JSON 形式のレスポンスを受け取ることになります。例えば、次のような具合です。

```
$ curl -i -H "Accept: application/json; q=1.0, */*; q=0.1" "http://localhost/users"

HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.20
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self,
      <http://localhost/users?page=2>; rel=next,
      <http://localhost/users?page=50>; rel=last
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8

[
    {
        "id": 1,
        ...
    },
    {
        "id": 2,
        ...
    },
    ...
]
```

舞台裏では、RESTful API コントローラ・アクションが実行される前に、[`ContentNegotiator`](/docs/api/yii/filters/contentnegotiator/index.md) フィルタがリクエストの `Accept` HTTP ヘッダをチェックして、[レスポンス形式](/docs/api/yii/web/response/index.md#property-format) を `'json'` に設定します。
アクションが実行されて、その結果のリソースのオブジェクトまたはコレクションが返されると、[`Serializer`](/docs/api/yii/rest/serializer/index.md) が結果を配列に変換します。
そして最後に、[`JsonResponseFormatter`](/docs/api/yii/web/jsonresponseformatter/index.md) が配列を JSON 文字列に変換して、それをレスポンス・ボディに入れます。

デフォルトでは、RESTful API は JSON と XML の両方の形式をサポートします。
新しい形式をサポートするためには、下記のように、API コントローラ・クラスの中で `contentNegotiator` フィルタの [formats](/docs/api/yii/filters/contentnegotiator/index.md#property-formats) プロパティを構成しなければなりません。

```php
use yii\web\Response;

public function behaviors()
{
    $behaviors = parent::behaviors();
    $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_HTML;
    return $behaviors;
}
```

`formats` プロパティのキーはサポートされる MIME タイプであり、値は対応するレスポンス形式名です。
このレスポンス形式名は、[`Response::formatters`](/docs/api/yii/web/response/index.md#property-formatters) の中でサポートされているものでなければなりません。

## データのシリアライズ <span id="data-serializing"></span>

上記で説明したように、[`Serializer`](/docs/api/yii/rest/serializer/index.md) が、リソースのオブジェクトやコレクションを配列に変換する際に、中心的な役割を果たします。
`Serializer` は、[`Arrayable`](/docs/api/yii/base/arrayable/index.md) および [`DataProviderInterface`](/docs/api/yii/data/dataproviderinterface/index.md) のインタフェイスを実装したオブジェクトを認識します。
前者は主としてリソース・オブジェクトによって実装され、後者はリソース・コレクションによって実装されています。

[`Controller::serializer`](/docs/api/yii/rest/controller/index.md#property-serializer) プロパティに構成情報配列をセットしてシリアライザを構成することが出来ます。
例えば、場合によっては、クライアントの開発作業を単純化するために、ページネーション情報をレスポンス・ボディに直接に含ませたいことがあるでしょう。
そうするためには、[`Serializer::collectionEnvelope`](/docs/api/yii/rest/serializer/index.md#property-collectionEnvelope) プロパティを次のように構成します。

```php
use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];
}
```

このようにすると、`http://localhost/users` というリクエストに対して、次のレスポンスを得ることが出来ます。

```
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.20
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self,
      <http://localhost/users?page=2>; rel=next,
      <http://localhost/users?page=50>; rel=last
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8

{
    "items": [
        {
            "id": 1,
            ...
        },
        {
            "id": 2,
            ...
        },
        ...
    ],
    "_links": {
        "self": {
            "href": "http://localhost/users?page=1"
        },
        "next": {
            "href": "http://localhost/users?page=2"
        },
        "last": {
            "href": "http://localhost/users?page=50"
        }
    },
    "_meta": {
        "totalCount": 1000,
        "pageCount": 50,
        "currentPage": 1,
        "perPage": 20
    }
}
```

### JSON 出力を制御する

JSON 形式のレスポンスを生成する [JsonResponseFormatter](/docs/api/yii/web/jsonresponseformatter/index.md) クラスは [JSON ヘルパ](/docs/api/yii/helpers/json/index.md) を内部的に使用します。
このフォーマッタはさまざまなオプションによって構成することが可能です。
例えば、[$prettyPrint](/docs/api/yii/web/jsonresponseformatter/index.md#property-prettyPrint) オプションは、より読みやすいレスポンスのためのもので、開発時に有用なオプションです。
また、[$encodeOptions](/docs/api/yii/web/jsonresponseformatter/index.md#property-encodeOptions) によって JSON エンコーディングの出力を制御することが出来ます。

フォーマッタは、以下のように、アプリケーションの [構成情報](/docs/ja/guide/concept-configurations/index.md) の中で、`response` アプリケーション・コンポーネントの [formatters](/docs/api/yii/web/response/index.md#property-formatters) プロパティの中で構成することが出来ます。

```php
'response' => [
    // ...
    'formatters' => [
        \yii\web\Response::FORMAT_JSON => [
            'class' => 'yii\web\JsonResponseFormatter',
            'prettyPrint' => YII_DEBUG, // デバッグ・モードでは "きれい" な出力を使用
            'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
            // ...
        ],
    ],
],
```

[DAO](/docs/ja/guide/db-dao/index.md) データベース・レイヤを使ってデータベースからデータを返す場合は、全てのデータが文字列として表されます。
しかし、特に数値は JSON では数として表現されなければなりませんので、これは必ずしも期待通りの結果であるとは言えません。
一方、ActiveRecord レイヤを使ってデータベースからデータを取得する場合は、数値カラムの値は、[`ActiveRecord::populateRecord()`](/docs/api/yii/db/activerecord/index.md#method-populateRecord) においてデータベースからデータが取得される際に、整数に変換されます。