Yii2 Request
php获取请求变量一般用$_POST,$_GET获取,从
表单或 url 中。Yii2把他封装在Request 组件中
注释部分是相当于
<?php $id = Yii::$app->request->get('id'); // 相当于 $id = isset($_GET['id']) ? $_GET['id'] : null; $id = Yii::$app->request->get('id', 1); // $id = isset($_GET['id']) ? $_GET['id'] : 1; $post = Yii::$app->request->post(); // $post = $_POST; $name = Yii::$app->request->post('name'); // $name = isset($_POST['name']) ? $_POST['name'] : null; $name = Yii::$app->request->post('name', ''); // $name = isset($_POST['name']) ? $_POST['name'] :
请求判断方法
<?php $request = Yii::$app->request; if ($request->isAjax) { /* ajax动作 */ } if ($request->isGet) { /* GET动作 */ } if ($request->isPost) { /* POST动作 */ } if ($request->isPut) { /* PUT动作 */ }
请求 URL
<?PHP // 请求url http://example.com/admin/index.php/product?id=100 $request = Yii::$app->request; echo $request->url; // /admin/index.php/product?id=100 echo $request->absoluteUrl; // http://example.com/admin/index.php/product?id=100 echo $request->hostInfo;// http://example.com echo $request->pathInfo;// /product echo $request->queryString; // id=100 echo $request->baseUrl; // /admin echo $request->scriptUrl; // /admin/index.php echo $request->serverName; // example.com echo $request->serverPort; //80 echo $request->userHost //用户主机名 echo $request->userIP; //用户IP
评论区
请登陆 后评论!
ABC001
very good
2021-10-08 21:55