json yii2 mysql_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 functiongetBodyParams()

{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 (!($parserinstanceof 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 (!($parserinstanceof 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[];

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值