Yii-http-requests

提供:Dev Guides
移動先:案内検索

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 *- *actionTestGet 関数を基本的なアプリケーションテンプレートの SiteController に追加します。

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

ステップ2 *- *http://localhost:8080/index.php?r = site/testget&id = 1&name = finddevguides&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 = finddevguides&message = welcome 」と入力すると、次のように表示されます。

Actiontestget関数出力の変更

*ステップ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 = finddevguides&message = welcome にアクセスすると、次のコードに示すような出力が表示されます。

修正されたActiontestget関数の出力

クライアントマシンのホスト名とIPアドレスを取得するには、 userHost および userIP プロパティを使用します。

ステップ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関数の出力画面