Yii2 配置request组件解析 json数据

在基础版本的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 [];
        }
    }

 

posted @ 2017-09-10 16:47  哦先生  阅读(8051)  评论(0编辑  收藏  举报