1. 程式人生 > >Yii2 配置request組件解析 json數據

Yii2 配置request組件解析 json數據

php and mine eat [] charset post function 格式

在基礎版本的config目錄下 web.php 或者高級版config目錄下的main.php中配置

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

在使用Yii::$app->request->post()時

調用yii\web\Request 中的post方法 :

 public function post($name = null
, $defaultValue = null) { if ($name === null) { return $this->getBodyParams(); } else { return $this->getBodyParam($name, $defaultValue); }+6

調用yii\web\Request 中的getBodyParams方法,解析內容是判斷request組件中有沒有相關content-type類型的解析器配置,有的話通過Yii ::createObject() 根據配置創建實例,調用相關解析器實例的parse方法解析數據內容,解析的數據內容是通過file_get_contents(‘php://input‘) 獲取的:

/**
     * Returns the request parameters given in the request body.
     *
     * Request parameters are determined using the parsers configured in [[parsers]] property.
     * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()`
     * to parse the [[rawBody|request body]].
     * @return array the request parameters given in the request body.
     * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]].
     * @see getMethod()
     * @see getBodyParam()
     * @see setBodyParams()
     
*/ public function getBodyParams() { if ($this->_bodyParams === null) { if (isset($_POST[$this->methodParam])) {
          //post提交時,存在以$this->methodParam(默認是_method 名稱)命名的參數時,直接把$_POST內容賦值給$this_bodyParams,同時刪除$this->methodParam內容
$this->_bodyParams = $_POST; unset($this->_bodyParams[$this->methodParam]); return $this->_bodyParams; }        //獲取Content-Type    $rawContentType = $this->getContentType(); if (($pos = strpos($rawContentType, ‘;‘)) !== false) { // e.g. application/json; charset=UTF-8 $contentType = substr($rawContentType, 0, $pos); } else { $contentType = $rawContentType; } if (isset($this->parsers[$contentType])) {
          //在request組件中存在相關$contentType類型配置時,創建該解析器,解析$this->getRawBody() 獲取的內容
          // $this->getRawBody() 是通過
file_get_contents(‘php://input‘)獲取內容
$parser = Yii::createObject($this->parsers[$contentType]);
                if (!($parser instanceof RequestParserInterface)) {
                    throw new InvalidConfigException("The ‘$contentType‘ request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
                }
                $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);
            } elseif (isset($this->parsers[‘*‘])) {
                $parser = Yii::createObject($this->parsers[‘*‘]);
                if (!($parser instanceof RequestParserInterface)) {
                    throw new InvalidConfigException("The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
                }
                $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);
            } elseif ($this->getMethod() === ‘POST‘) {
                // PHP has already parsed the body so we have all params in $_POST
                $this->_bodyParams = $_POST;
            } else {
                $this->_bodyParams = [];
                mb_parse_str($this->getRawBody(), $this->_bodyParams);
            }
        }

        return $this->_bodyParams;
    }
yii\web\JsonParser json 解析器,是通過parse方法解析獲取的json內容,主要是通過框架裏面加強版的json_decode() 就是 yii\helpers\Json::decode($rawBody, $this->asArray) 把時間josn數據變為數組格式
 /**
     * Parses a HTTP request body.
     * @param string $rawBody the raw HTTP request body.
     * @param string $contentType the content type specified for the request body.
     * @return array parameters parsed from the request body
     * @throws BadRequestHttpException if the body contains invalid json and [[throwException]] is `true`.
     */
    public function parse($rawBody, $contentType)
    {
        try {
            $parameters = Json::decode($rawBody, $this->asArray);
            return $parameters === null ? [] : $parameters;
        } catch (InvalidParamException $e) {
            if ($this->throwException) {
                throw new BadRequestHttpException(‘Invalid JSON data in request body: ‘ . $e->getMessage());
            }
            return [];
        }
    }

Yii2 配置request組件解析 json數據