リクエストインスタンスの取得Obtaining A Request Instance
ファサードによる取得Via Facade
Requestファサードは、コンテナにより結合されている、現在のリクエストへアクセスさせてくれます。例えば:The Request facade will grant you access to the current request that is bound in the container. For example:
$name = Request::input('name');
名前空間下では、クラスファイルの初めで、user Request文により、Requestファサードをインポートする必要があることを覚えておいてください。Remember, if you are in a namespace, you will have to import the Request facade using a use Request; statement at the top of your class file.
依存注入による取得Via Dependency Injection
依存注入により、現在のHTTPリクエストインスタンスを取得するには、コントローラーのコンストラクターかメソッドに、タイプヒントにより指定します。現在のリクエストインスタンスは、サービスプロバイダーにより、自動的に注入されます。To obtain an instance of the current HTTP request via dependency injection, you should type-hint the class on your controller constructor or method. The current request instance will automatically be injected by the service container[/docs/master/container]:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller {
	/**
	 * 新ユーザーの保存
	 *
	 * @param  Request  $request
	 * @return Response
	 */
	public function store(Request $request)
	{
		$name = $request->input('name');
		//
	}
}
もし、コントローラーメソッドで、ルートパラメーターも併用したい場合は、依存の指定の後にルート引数を続けてリストしてください。If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller {
	/**
	 * 新ユーザーの保存
	 *
	 * @param  Request  $request
	 * @param  int  $id
	 * @return Response
	 */
	public function update(Request $request, $id)
	{
		//
	}
}
入力の取得Retrieving Input
一つの値を取得するRetrieving An Input Value
Illuminate\Http\Requestインスタンスのシンプルなメソッドを利用すれば、ユーザー入力の全てにアクセスできます。リクエストのHTTP動詞に気をもむ必要はありません。全ての動詞の入力に対し、同じ方法でアクセスできます。Using a few simple methods, you may access all user input from your Illuminate\Http\Request instance. You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs.
$name = Request::input('name');
入力値が存在しない場合に、デフォルト値を取得するRetrieving A Default Value If The Input Value Is Absent
$name = Request::input('name', 'Sally');
入力値が存在するか確かめるDetermining If An Input Value Is Present
if (Request::has('name'))
{
	//
}
リクエストの全入力を取得するGetting All Input For The Request
$input = Request::all();
限定されたリクエスト入力のみ取得するGetting Only Some Of The Request Input
$input = Request::only('username', 'password');
$input = Request::except('credit_card');
「配列」入力を使ったフォームを動作させる場合、ドット記法を使い、配列にアクセスできます。When working on forms with "array" inputs, you may use dot notation to access the arrays:
$input = Request::input('products.0.name');
直前の入力Old Input
さらにLaravelでは、現在のリクエストの入力を次のリクエストに持ち越すこともできます。例えば、入力をチェックし、バリデーションエラーがあった場合、フォームへ入力された値を再表示する必要があるでしょう。Laravel also allows you to keep input from one request during the next request. For example, you may need to re-populate a form after checking it for validation errors.
フラッシュデーターをセッションへ保存Flashing Input To The Session
flashメソッドは、アプリケーションの次のリクエストでも、現在の入力へアクセスできるように、セッションへフラッシュデーターとして保存します。The flash method will flash the current input to the session[/docs/master/session] so that it is available during the user's next request to the application:
Request::flash();
一部のデーターをフラッシュデーターとして保存Flashing Only Some Input To The Session
Request::flashOnly('username', 'email');
Request::flashExcept('password');
フラッシュデーターとリダイレクトFlash & Redirect
多くの場合、前のページへのリダイレクトにフラッシュデーターを一緒に付けることが多いですので、リダイレクトにチェーンし、入力をフラッシュデーターとして保存できます。Since you often will want to flash input in association with a redirect to the previous page, you may easily chain input flashing onto a redirect.
return redirect('form')->withInput();
return redirect('form')->withInput(Request::except('password'));
直前の入力の取得Retrieving Old Data
前のリクエストで保存したフラッシュデーターを取得するには、Requestインスタンスのoldメソッドを使用してください。To retrieve flashed input from the previous request, use the old method on the Request instance.
$username = Request::old('username');
Bladeテンプレートの中で、直前の入力を表示する場合は、oldヘルパを使い、便利に表示できます。If you are displaying old input within a Blade template, it is more convenient to use the old helper:
{{ old('username') }}
クッキーCookies
Laravelフレームワークが作るクッキーは全部、認証コードを利用し暗号化されています。つまり、クライアントが変更すると、無効なクッキーだと判断します。All cookies created by the Laravel framework are encrypted and signed with an authentication code, meaning they will be considered invalid if they have been changed by the client.
クッキー値の取得Retrieving A Cookie Value
$value = Request::cookie('name');
新クッキーをレスポンスへ追加Attaching A New Cookie To A Response
cookieヘルパは、新しいSymfony\Component\HttpFoundation\Cookieインスタンスを生成する、シンプルなファクトリーとして動作します。そのクッキーは、withCookieメソッドを使用し、Responseインスタンスに付けます。The cookie helper serves as a simple factory for generating new Symfony\Component\HttpFoundation\Cookie instances. The cookies may be attached to a Response instance using the withCookie method:
$response = new Illuminate\Http\Response('Hello World');
$response->withCookie(cookie('name', 'value', $minutes));
永遠に残るクッキーの作成Creating A Cookie That Lasts Forever*
永遠とは、実際には5年間を意味しています。By "forever", we really mean five years.
$response->withCookie(cookie()->forever('name', 'value'));
ファイルFiles
アップデートしたファイルを取得するRetrieving An Uploaded File
$file = Request::file('photo');
ファイルがアップロードされたか調べるDetermining If A File Was Uploaded
if (Request::hasFile('photo'))
{
	//
}
fileメソッドがリターンするのは、Symfony\Component\HttpFoundation\File\UploadedFileクラスのインスタンスで、PHPのSplFileInfoを拡張しています。それにはファイルに関する様々なメソッドが提供されています。The object returned by the file method is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile class, which extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file.
アップロードされたファイルが有効であるか調べるDetermining If An Uploaded File Is Valid
if (Request::file('photo')->isValid())
{
	//
}
アップロードしたファイルを削除するMoving An Uploaded File
Request::file('photo')->move($destinationPath);
Request::file('photo')->move($destinationPath, $fileName);
他のファイルメソッドOther File Methods
他にも様々なメソッドが、UploadedFileインスタンスに用意されています。更に知りたい場合は、このクラスのAPIドキュメントを確認してください。There are a variety of other methods available on UploadedFile instances. Check out the API documentation for the class[http://api.symfony.com/2.5/Symfony/Component/HttpFoundation/File/UploadedFile.html] for more information regarding these methods.
その他の情報Other Request Information
Requestクラスはアプリケーションに対するHTTPリクエストを調べるための多くのメソッドを提供しており、Symfony\Component\HttpFoundation\Requestを拡張しています。その特徴のいくつかをご覧ください。The Request class provides many methods for examining the HTTP request for your application and extends the Symfony\Component\HttpFoundation\Request class. Here are some of the highlights.
リクエストされたURIを取得するRetrieving The Request URI
$uri = Request::path();
リクエストのメソッドを取得するRetrieving The Request Method
$method = Request::method();
if (Request::isMethod('post'))
{
	//
}
リクエスされたパスがパターンと一致するか調べるDetermining If The Request Path Matches A Pattern
if (Request::is('admin/*'))
{
	//
}
現在のリクエストURLを取得するGet The Current Request URL
$url = Request::url();