リクエストインスタンスの取得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/{{version}}/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/{{version}}/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'));
クッキーのキューQueueing Cookies
実際にレスポンスが生成される前の状態でも、送り返されるレスポンスにクッキーを付けるために、キュー(queue)しておくこともできます。You may also "queue" a cookie to be added to the outgoing response, even before that response has been created:
<?php namespace App\Http\Controllers;
use Cookie;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Update a resource
*
* @return Response
*/
public function update()
{
Cookie::queue('name', 'value');
return response('Hello World');
}
}
ファイル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();
リクエストがAJAXであるか判別Determine If The Request Is Using AJAX
if (Request::ajax())
{
//
}
リクエストのメソッド取得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();