Yii HTTP请求
请求由 yii\web\Request 对象表示,它提供有关HTTP标头、请求参数、Cookie等的信息。
get() 和 post() 方法返回请求组件的请求参数。
示例 −
步骤1 - 在基本应用模板的SiteController中添加一个 actionTestGet 函数。
步骤2 - 现在转到 **http://localhost: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 函数如下代码所示。
步骤4 - 前往 http://localhost:8080/index.php?r=site/test-get 。您将看到以下内容。
请求组件提供了许多属性来检查请求的URL。
步骤5 - 修改 actionTestGet 函数如下。
步骤6 − 在Web浏览器的地址栏中,输入 **http://localhost:8080/index.php?r=site/testget &id=1&name=tutorialspoint&message=welcome ** ,您将看到以下内容。
步骤7 - 要获取HTTP头信息,您可以使用 yii\web\Request::$headers 属性。以以下方式修改 actionTestGet 函数。
步骤8 − 如果你访问以下URL **http://localhost:8080/index.php?r=site/testget &id=1&name=tutorialspoint&message=welcome ** ,你将看到以下代码显示的输出结果。
要获取客户机的主机名和IP地址,请使用 userHost 和 userIP 属性。
步骤9 - 以这种方式修改 actionTestGet 函数。
步骤10 - 转到以下地址 http://localhost:8080/index.php?r=site/test-get ,您会看到以下屏幕。