8.Yii2.0框架控制器接收get.post数据
8.Yii2.0框架控制器接收get.post数据
一.get传参
1 <?php 2 /** 3 * Created by Haima. 4 * Author:Haima 5 * QQ:228654416 6 * Date: 2018/8/23 7 * Time: 5:36 8 */ 9 10 namespace app\controllers; 11 12 use yii\base\Controller; 13 14 class HomeController extends Controller 15 { 16 public function actionIndex(){ 17 $request = \Yii::$app->request; 18 //获取get传参 19 $id = $request->get('id',1); //默认不传参时为1 20 dump($id); 21 //获取post传参 22 // $username = $request->post('username','xiaoli'); //默认不传参时为xiaoli 23 // dump($username); 24 //// return $this->render('index'); 25 } 26 }
get打印效果:
http://yii.com/index.php?r=home/index&id=6
这里的index可以不要
例:
http://yii.com?r=home/index&id=6
二.post传参:
1 <?php 2 /** 3 * Created by Haima. 4 * Author:Haima 5 * QQ:228654416 6 * Date: 2018/8/23 7 * Time: 5:36 8 */ 9 10 namespace app\controllers; 11 12 use yii\base\Controller; 13 14 class HomeController extends Controller 15 { 16 public function actionIndex(){ 17 $request = \Yii::$app->request; 18 //获取get传参 19 // $id = $request->get('id',1); //默认不传参时为1 20 // dump($id); 21 //获取post传参 22 $username = $request->post('username','xiaoli'); //默认不传参时为xiaoli 23 dump($username); 24 // return $this->render('index'); 25 } 26 }
打印效果
三.判断请求类型和用户IP:
1 <?php 2 /** 3 * Created by Haima. 4 * Author:Haima 5 * QQ:228654416 6 * Date: 2018/8/23 7 * Time: 5:36 8 */ 9 10 namespace app\controllers; 11 12 use yii\base\Controller; 13 14 class HomeController extends Controller 15 { 16 public function actionIndex(){ 17 $request = \Yii::$app->request; 18 //获取get传参 19 // $id = $request->get('id',1); //默认不传参时为1 20 // dump($id); 21 //获取post传参 22 // $username = $request->post('username','xiaoli'); //默认不传参时为xiaoli 23 $get = $request->isGet; //判断get提交 返回 true/false 24 $post = $request->isPost; //判断post提交 返回 true/false 25 $ip = $request->UserIP; //获取用户IP 26 27 vp($get); 28 vp($post); 29 vp($ip); 30 // return $this->render('index'); 31 } 32 }
打印效果:
[Haima的博客]
http://www.cnblogs.com/haima/