前回に引き続いてリクエストの話です。ブラウザがないとリクエスト(Request)のオブジェクトが作成できない、とは限りません。Laravelならコマンドラインで作成できます。どうやって?

リクエストオブジェクトの作成

コマンドラインと言えばもちろん、tinkerのことです。以下のように、クラスのcreate()メソッドを使って、リクエストのオブジェクトを作成できます。

Psy Shell v0.9.3 (PHP 7.1.14 — cli) by Justin Hileman

>>> $request = \Illuminate\Http\Request::create('http://localhost', 'GET', ['name' => 'test']);

=> Illuminate\Http\Request {#2324
     ⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {#2309
       #message: "Unexpected ErrorException thrown from a caster: Undefined index: ",
     },
     +attributes: Symfony\Component\HttpFoundation\ParameterBag {#2320},
     +request: Symfony\Component\HttpFoundation\ParameterBag {#2322},
     +query: Symfony\Component\HttpFoundation\ParameterBag {#2321},
     +server: Symfony\Component\HttpFoundation\ServerBag {#2317},
     +files: Symfony\Component\HttpFoundation\FileBag {#2318},
     +cookies: Symfony\Component\HttpFoundation\ParameterBag {#2319},
     +headers: Symfony\Component\HttpFoundation\HeaderBag {#2316},
   }

Laravelのリクエストは、Symfonyのクラスがベースになっているようです。createは、Laravelのクラスのメソッドではなく、Symfonyのクラスのメソッドです。参考のために、以下にその定義の一部を掲載します。

    /**
     * Creates a Request based on a given URI and configuration.
     *
     * The information contained in the URI always take precedence
     * over the other information (server and parameters).
     *
     * @param string               $uri        The URI
     * @param string               $method     The HTTP method
     * @param array                $parameters The query (GET) or request (POST) parameters
     * @param array                $cookies    The request cookies ($_COOKIE)
     * @param array                $files      The request files ($_FILES)
     * @param array                $server     The server parameters ($_SERVER)
     * @param string|resource|null $content    The raw body data
     *
     * @return static
     */
    public static function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
    {
    ..

先のtinkerの実行では、create()の3番目のパラメータに、連想配列で値(['name' => 'test'])を渡しました。ということは、以下のtinkerの実行で変数の取り出し可能です。

>>> $request->all();

=> [
     "name" => "test",
   ]

>>> $request->input();

=> [
     "name" => "test",
   ]

>>> $request->name;

=> "test"

>>> $request->input('name');

=> "test"

>>> $request->get('name');

=> "test"
>>> 

前回に説明したことが、ここで簡単にシミュレートできるのです。

そして、さらに、こんなこともテストできます。簡単なValidationのテストです。

>>> $request->validate(['name' => 'required']);

=> [
     "name" => "test",
   ]

>>> $request->validate(['name' => 'required|alpha']);

=> [
     "name" => "test",
   ]

>>> $request->validate(['name' => 'alpha']);
=> [
     "name" => "test",
   ]

>>> $request->validate(['name' => 'numeric']);
Illuminate/Validation/ValidationException with message 'The given data was invalid.'
>>> 

Validationに成功したら、入力の連想配列を返し、失敗したらValidationExceptionを返します。

tinkerの便利さは、いちいちプログラムを書かずに、ブラウザを立ち上げることなしに、テストできることです。こんな便利なツールを習得せずに、Laravelを語ることはできません。

By khino