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

actionTestGet Function Output

要檢索其他請求方法(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**。您將看到以下內容。

Get Request

請求元件提供了許多屬性來檢查請求的 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**,您將看到以下內容。

Modify Actiontestget Function Output

**步驟 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**,您將看到如下所示的輸出。

Modified Actiontestget Function Output

要獲取客戶端機器的主機名和 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**,您將看到以下螢幕。

actionTestGet Function Output Screen
廣告