
- 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**,您將看到以下螢幕。
