In the basic version of Config directory web.php or the advanced version of Config directory under the main.php configuration
' Components ' + = [' request ' = ' = ' parsers ' = ' = ' Application/json ' = > ' Yii\web\jsonparser ', ], ],
When using Yii:: $app->request->post ()
Call the Post method in Yii\web\request:
Public functionPost$name=NULL,$defaultValue=NULL) { if($name===NULL) { return $this-Getbodyparams (); } Else { return $this->getbodyparam ($name,$defaultValue); }+6
Invoking the Getbodyparams method in Yii\web\request, parsing the content is to determine whether there is a parser configuration in the Request component that does not have the relevant content-type type, and, if so, by Yii:: CreateObject () creates an instance from the configuration , the parse method that invokes the relevant parser instance parses the data content, and the parsed data is obtained through file_get_contents (' Php://input '):
/** * Returns the request parameters given in the request body. * * Request parameters is determined using the parsers configured in [[Parsers]] property. * If no parsers is configured for the current [[ContentType]] It uses the PHP function ' mb_parse_str () ' * To parse th e [[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])) {
When the post commits, there is a parameter named $this->methodparam (the default is the _method name), the $_post content is assigned to $this_bodyparams directly, and the $this-> is deleted. Methodparam content$this->_bodyparams =$_post; unset($this->_bodyparams[$this-Methodparam]); return $this-_bodyparams; }//Get 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])) {
When there is an associated $contenttype type configuration in the request component, the parser is created to parse the content obtained by $this->getrawbody ()
$this->getrawbody () is passedfile_get_contents (' php://input ') get content
$parser= Yii::createobject ($this->parsers[$contentType]); if(! ($parserinstanceof Requestparserinterface)) { Throw NewInvalidconfigexception ("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 NewInvalidconfigexception ("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 had already parsed the body so we had all params in $_post $this->_bodyparams =$_post; } Else { $this->_bodyparams = []; Mb_parse_str ($this->getrawbody (),$this-_bodyparams); } } return $this-_bodyparams; }
The Yii\web\jsonparser JSON parser, which is the JSON content parsed by the parse method, is mainly through the Json_decode () in the framework of the Yii\helpers\json::d ecode ($rawBody, $this->asarray) Turn the time Josn data into an array format
/** * 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 functionParse$rawBody,$contentType) { Try { $parameters= Json::d ecode ($rawBody,$this-Asarray); return $parameters===NULL? [] :$parameters; } Catch(invalidparamexception$e) { if($this-throwexception) { Throw NewBadrequesthttpexception (' Invalid JSON data in Request body: '.$e-getMessage ()); } return []; } }
YII2 Configuring the request component to parse JSON data