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步 - 现在转到 http:// localhost:8080 / index.php?r = site / testget&id = 1&name = codingdict&message = welcome ,您将看到以下内容。

actionTestGet函数输出

要检索其他请求方法(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步 - 在Web浏览器的地址栏中,输入 http:// localhost:8080 / index.php?r = site / testget&id = 1&name = codingdict&message = welcome ,您将看到以下内容。

![修改Actiontestget函数输出](./Yii
HTTP请求_files/modify_actiontestget_function_output.jpg)

第7步 - 要获取HTTP标头信息,您可以使用 yii \ web \ Request :: $标头 属性。以这种方式修改 actionTestGet 函数。

public function actionTestGet() {
   var_dump(Yii::$app->request->headers);
}

第8步 - 如果您转到URL http:// localhost:8080 / index.php?r = site / testget&id = 1&name = codingdict&message = welcome ,您将看到输出,如下面的代码所示。

![修改后的Actiontestget函数输出](/static/assets/tutorials/php/yii/modified_actiontestget_function_output.jpg)

要获取客户机的主机名和IP地址,请使用 userHostuserIP 属性。

第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 ,您会看到以下屏幕。

actionTestGet函数输出屏幕

当Web应用程序处理请求时,它会生成一个响应对象,其中包含HTTP标头,正文和HTTP状态码。在大多数情况下,您将使用响应应用程序组件。默认情况下,它是 yii \ web \ Response的 一个实例。要管理响应HTTP ...