0 フォロワー

クラス yii\web\JsonParser

継承yii\web\JsonParser
実装yii\web\RequestParserInterface
利用可能なバージョン2.0
ソースコード https://github.com/yiisoft/yii2/blob/master/framework/web/JsonParser.php

yii\helpers\Json::decode()を使用して、生のHTTPリクエストを解析します。

JSONリクエストの解析を有効にするには、このクラスを使用してyii\web\Request::$parsersを設定できます。

'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

公開プロパティ

継承されたプロパティを隠す

プロパティ 説明 定義元
$asArray boolean 連想配列の形式でオブジェクトを返すかどうか。 yii\web\JsonParser
$throwException boolean ボディが無効なJSONの場合、yii\web\BadRequestHttpExceptionをスローするかどうか yii\web\JsonParser

公開メソッド

継承されたメソッドを隠す

メソッド 説明 定義元
parse() HTTPリクエストボディを解析します。 yii\web\JsonParser

プロパティ詳細

継承されたプロパティを隠す

$asArray public プロパティ

連想配列の形式でオブジェクトを返すかどうか。

public boolean $asArray true
$throwException public プロパティ

ボディが無効なJSONの場合、yii\web\BadRequestHttpExceptionをスローするかどうか

public boolean $throwException true

メソッド詳細

継承されたメソッドを隠す

parse() public メソッド

HTTPリクエストボディを解析します。

public array|stdClass parse ( $rawBody, $contentType )
$rawBody string

生のHTTPリクエストボディ。

$contentType string

リクエストボディに指定されたコンテンツタイプ。

return array|stdClass

リクエストボディから解析されたパラメータ

throws yii\web\BadRequestHttpException

ボディに無効なjsonが含まれており、$throwExceptiontrueの場合。

                public function parse($rawBody, $contentType)
{
    // converts JSONP to JSON
    if (strpos($contentType, 'application/javascript') !== false) {
        $rawBody = preg_filter('/(^[^{]+|[^}]+$)/', '', $rawBody);
    }
    try {
        $parameters = Json::decode($rawBody, $this->asArray);
        return $parameters === null ? [] : $parameters;
    } catch (InvalidArgumentException $e) {
        if ($this->throwException) {
            throw new BadRequestHttpException('Invalid JSON data in request body: ' . $e->getMessage());
        }
        return [];
    }
}