Analysis of Yii Framework Processing get and post Request by Request Component

  • 2021-12-19 06:07:51
  • OfStack

This paper illustrates the method of Yii framework processing get and post requests through request components. Share it for your reference, as follows:

When processing get and post requests in the operation of the controller, the request component needs to be obtained first.


$request = \Yii::$app->request;

After getting this request component, we can get the parameters through the request component.


// Pass get Get parameters 
$id = $request->get("id");
// Pass post Get parameters 
$id = $request->post("id");

In the Yii framework, we can not only get the parameter, but also set the default value. If this parameter is not in the pass parameter, the default value will be returned.


// For get,post Two ways to set default parameters 10
$id = $request->get("id",10);
$id = $request->post("id",10);

At this time, if you visit http://basic/web/index. php? r=index/say? When num = 20, $id gets the default value of 10 because there is no id in the parameter.

In this $request component, basic judgments and so on are also provided, such as how to judge the request.


if($request->isGet){
  echo "this is Get";
}else if ($request->isPost){
  echo "this is Post";
}

If the request is in Get mode, it will print out

this is Get

If it is Post, it will output

this is Post

The request component can also obtain the user's ip address and other information. Here, take the IP address as an example


$user_ip = $request->userIP;

For more readers interested in Yii related contents, please check the topics on this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Yii framework.


Related articles: