- Yii 教程
 - Yii - 主页
 - Yii - 概述
 - Yii - 安装
 - Yii - 创建页面
 - Yii - 应用程序结构
 - Yii - 入口脚本
 - Yii - 控制器
 - Yii - 使用控制器
 - Yii - 使用动作
 - Yii - 模型
 - Yii - 小部件
 - Yii - 模块
 - Yii - 视图
 - Yii - 布局
 - Yii - 资产
 - Yii - 资产转换
 - Yii - 扩展
 - Yii - 创建扩展
 - Yii - HTTP 请求
 - Yii - 响应
 - Yii - URL 格式
 - Yii - URL 路由
 - Yii - URL 规则
 - Yii - HTML 表单
 - Yii - 验证
 - Yii - 临时验证
 - Yii - AJAX 验证
 - Yii - 会话
 - Yii - 使用闪存数据
 - Yii - cookie
 - Yii - 使用 Cookie
 - Yii - 文件上传
 - Yii - 格式化
 - Yii - 分页
 - Yii - 排序
 - Yii - 属性
 - Yii - 数据提供者
 - Yii - 数据小部件
 - Yii - 列表视图小部件
 - Yii - GridView 小部件
 - Yii - 活动
 - Yii - 创建事件
 - Yii - Behave
 - Yii - 创建Behave
 - Yii - 配置
 - Yii - 依赖注入
 - Yii - 数据库访问
 - Yii - 数据访问对象
 - Yii - 查询生成器
 - Yii - 活动记录
 - Yii - 数据库迁移
 - Yii - 主题化
 - Yii - RESTful API
 - Yii - RESTful API 的实际应用
 - Yii - 字段
 - Yii - 测试
 - Yii - 缓存
 - Yii - 片段缓存
 - Yii - 别名
 - Yii - 日志记录
 - Yii - 错误处理
 - Yii - 身份验证
 - Yii - 授权
 - Yii - 本地化
 - Yii-Gii
 - Gii – 创建模型
 - Gii – 生成控制器
 - Gii – 生成模块
 
- Yii 有用的资源
 - Yii - 快速指南
 - Yii - 有用的资源
 - Yii - 讨论
 
Yii - HTTP 请求
请求由yii\web\Request对象表示,它提供有关 HTTP 标头、请求参数、cookie 等的信息。
get()和post()方法返回请求组件的请求参数。
示例-
$req = Yii::$app->request;
   /*
   * $get = $_GET;
   */
   $get = $req->get();
   /*
   * if(isset($_GET['id'])) {
   *     $id = $_GET['id'];
   * } else {
   *     $id = null;
   * }
   */
   $id = $req->get('id');
	
   /*
   * if(isset($_GET['id'])) {
   *     $id = $_GET['id'];
   * } else {
   *     $id = 1;
   * }
   */
   $id = $req->get('id', 1);
	
   /*
   * $post = $_POST;
	*/
   $post = $req->post();
   /*
   * if(isset($_POST['name'])) {       
   *     $name = $_POST['name'];          
   * } else {
   *     $name = null;
   * }
   */
   $name = $req->post('name');
		  
   /*
   * if(isset($_POST['name'])) {
   *     $name = $_POST['name'];
   * } else {
   *     $name = '';
   * }
   */
   $name = $req->post('name', '');
步骤 1 - 将actionTestGet函数添加到基本应用程序模板的SiteController中。
public function actionTestGet() {
   var_dump(Yii::$app->request->get());
}
步骤 2 - 现在转到http://localhost:8080/index.php?r=site/testget&id=1&name=tutorialspoint&message=welcome,您将看到以下内容。
要检索其他请求方法(PATCH、DELETE 等)的参数,请使用yii\web\Request::getBodyParam()方法。
要获取当前请求的 HTTP 方法,请使用Yii::$app→request→method属性。
步骤 3 - 修改actionTestGet函数,如以下代码所示。
public function actionTestGet() {
   $req = Yii::$app->request;
   if ($req->isAjax) {
      echo "the request is AJAX";
   }
   if ($req->isGet) {
      echo "the request is GET";
   }
   if ($req->isPost) {
      echo "the request is POST";
   }
   if ($req->isPut) {
      echo "the request is PUT";
   }
}
步骤 4 - 转到http://localhost:8080/index.php?r=site/test-get。您将看到以下内容。
请求组件提供了许多属性来检查所请求的 URL。
步骤 5 - 修改actionTestGet函数如下。
public function actionTestGet() {
   //the URL without the host
   var_dump(Yii::$app->request->url);
   
   //the whole URL including the host path
   var_dump(Yii::$app->request->absoluteUrl);
   
   //the host of the URL
   var_dump(Yii::$app->request->hostInfo);
   
   //the part after the entry script and before the question mark
   var_dump(Yii::$app->request->pathInfo);
   
   //the part after the question mark
   var_dump(Yii::$app->request->queryString);
   
   //the part after the host and before the entry script
   var_dump(Yii::$app->request->baseUrl);
   
   //the URL without path info and query string
   var_dump(Yii::$app->request->scriptUrl);
   
   //the host name in the URL
   var_dump(Yii::$app->request->serverName);
   
   //the port used by the web server
   var_dump(Yii::$app->request->serverPort);
}
步骤 6 - 在网络浏览器的地址栏中输入http://localhost:8080/index.php?r=site/testget&id=1&name=tutorialspoint&message=welcome,您将看到以下内容。
步骤 7 - 要获取 HTTP 标头信息,您可以使用yii\web\Request::$headers属性。这样修改actionTestGet函数。
public function actionTestGet() { 
   var_dump(Yii::$app->request->headers); 
}
步骤 8 - 如果您访问 URL http://localhost:8080/index.php?r=site/testget&id=1&name=tutorialspoint&message=welcome,您将看到如下代码所示的输出。
要获取客户端计算机的主机名和 IP 地址,请使用userHost和userIP属性。
步骤 9 -以这种方式修改actionTestGet函数。
public function actionTestGet() {
   var_dump(Yii::$app->request->userHost);
   var_dump(Yii::$app->request->userIP);
}
步骤 10 - 转到地址http://localhost:8080/index.php?r=site/test-get,您会看到以下屏幕。
