Yii2 配置request组件解析 json数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Yii2 配置request组件解析 json数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3797字,纯文字阅读大概需要6分钟。
内容图文
在基础版本的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)) { thrownew 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)) { thrownew 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) { thrownew BadRequestHttpException(‘Invalid JSON data in request body: ‘ . $e->getMessage()); } return []; } }
原文:http://www.cnblogs.com/xp796/p/7501456.html
内容总结
以上是互联网集市为您收集整理的Yii2 配置request组件解析 json数据全部内容,希望文章能够帮你解决Yii2 配置request组件解析 json数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。