Yii HTTP請求


Requests是 yii\web\Request 物件的表示,它提供了HTTP頭,請求引數,Cookies等資訊。
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步 - 現在開啟URL=> http://localhost:8080/index.php?r=site/test-get&id=1&name=yiibai&message=welcome , 
你會看到以下內容。


要檢索的另一個請求方法的引數 (PATCH, DELETE, etc.), 使用 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步-在Web瀏覽器位址列中輸入URL=>http://localhost:8080/index.php?r=site/testget&id=1&name=yiibai&message=welcome
你會看到以下內容。

Yii HTTP請求
第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=yiibai&message=welcome, 
你會看到如圖所示的程式碼輸出。

要獲得主機名和客戶機的IP地址,使用 userHost 和userIP 屬性。

第9步 - 需要像這樣修改 actionTestGet 函式。
public function actionTestGet() {
   var_dump(Yii::$app->request->userHost);
   var_dump(Yii::$app->request->userIP);
}
第10步- 轉到地址URL=> http://localhost:8080/index.php?r=site/test-get ,
會看到下面的輸出結果: