- 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 - ListView 小部件
- Yii - GridView 小部件
- Yii - 事件
- Yii - 创建事件
- Yii - 行为
- Yii - 创建行为
- 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** - 在基本应用程序模板的 **SiteController** 中添加 **actionTestGet** 函数。
public function actionTestGet() {
var_dump(Yii::$app->request->get());
}
**步骤 2** - 现在转到 **https://: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** - 转到 **https://: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** - 在网页浏览器的地址栏中,键入 **https://: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 **https://: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** - 转到地址 **https://:8080/index.php?r=site/test-get**,您将看到以下屏幕。