設定Configuration
HTTP駆動のアプリケーションはステートレスですから、リクエスト間に渡り、情報を保存するセッションが提供されています。Laravelは美しく、統一されたAPIでバックエンドの様々なセッションを用意しています。人気のあるMemcachedやRedis、データベースも最初からサポートしています。Since HTTP driven applications are stateless, sessions provide a way to store information about the user across requests. Laravel ships with a variety of session back-ends available for use through a clean, unified API. Support for popular back-ends such as Memcached[http://memcached.org], Redis[http://redis.io], and databases is included out of the box.
セッションの設定はconfig/session.phpにあります。このファイルのオプションには詳しくコメントがついていますので、確認して下さい。ほとんどのアプリケーションでうまく動作できるように、Laravelはfileセッションドライバーをデフォルトとして設定しています。The session configuration is stored in config/session.php. Be sure to review the well documented options available to you in this file. By default, Laravel is configured to use the file session driver, which will work well for the majority of applications.
LaravelでReidsセッションを使用する前に、Composerでpredis/predisパッケージ(~1.0)をインストールする必要があります。Before using Redis sessions with Laravel, you will need to install the predis/predis package (~1.0) via Composer.
注目: セッションに保存するデータを全て暗号化したい場合は、
encrypt設定オプションをtureに設定します。Note: If you need all stored session data to be encrypted, set theencryptconfiguration option totrue.
予約キーReserved Keys
Laravelは内部で、flashセッションキーを使用しています。そのため、この名前のセッションアイテムを追加しないでください。The Laravel framework uses the flash session key internally, so you should not add an item to the session by that name.
セッションの使用Session Usage
セッションにアイテムを保存するStoring An Item In The Session
Session::put('key', 'value');
値を配列セッション値として保存するPush A Value Onto An Array Session Value
Session::push('user.teams', 'developers');
セッションからアイテムを取得するRetrieving An Item From The Session
$value = Session::get('key');
アイテムかデフォルト値を取得するRetrieving An Item Or Returning A Default Value
$value = Session::get('key', 'default');
$value = Session::get('key', function() { return 'default'; });
アイテム取得後に削除するRetrieving An Item And Forgetting It
$value = Session::pull('key', 'default');
セッションから全データを取得するRetrieving All Data From The Session
$data = Session::all();
セッションにアイテムが存在するか確認するDetermining If An Item Exists In The Session
if (Session::has('users'))
{
	//
}
セッションからアイテムを削除するRemoving An Item From The Session
Session::forget('key');
セッションから全てのアイテムを削除するRemoving All Items From The Session
Session::flush();
セッションIDを再生成するRegenerating The Session ID
Session::regenerate();
フラッシュデーターFlash Data
次のリクエストの間だけアイテムを保存したいこともあります。Session::flashメソッドが使えます。Sometimes you may wish to store items in the session only for the next request. You may do so using the Session::flash method:
Session::flash('key', 'value');
現在のフラッシュデーターの寿命を次のリクエストまで延長するReflashing The Current Flash Data For Another Request
Session::reflash();
一部のフラッシュデーターの寿命を延長するReflashing Only A Subset Of Flash Data
Session::keep(array('username', 'email'));
データベースセッションDatabase Sessions
databaseセッションを使用する場合、セッションのアイテムを含んだテーブルを用意してください。以下にサンプルのSchema宣言を紹介します。When using the database session driver, you will need to setup a table to contain the session items. Below is an example Schema declaration for the table:
Schema::create('sessions', function($table)
{
	$table->string('id')->unique();
	$table->text('payload');
	$table->integer('last_activity');
});
もちろん、Artisanコマンドのsession:tableで、このマイグレーションを生成することも可能です!Of course, you may use the session:table Artisan command to generate this migration for you!
php artisan session:table
composer dump-autoload
php artisan migrate
セッションドライバーSession Drivers
セッション「ドライバー」は、それぞれのリクエストのセッションデーターをどこに保存しておくかを定義します。Laravelでは、最初から素晴らしいドライバーが用意されています。The session "driver" defines where session data will be stored for each request. Laravel ships with several great drivers out of the box:
- file- セッションは- app/storage/sessionsの中に保存されます。- file- sessions will be stored in- app/storage/sessions.
- cookie- セッションはセキュアに暗号化されたクッキーに保存されます。- cookie- sessions will be stored in secure, encrypted cookies.
- database- セッションはアプリケーションで使用されるデータベースに保存されます。- database- sessions will be stored in a database used by your application.
- memcached/- redis- セッションは指定された、早い、キャッシュベースの保存領域に保持されます。- memcached/- redis- sessions will be stored in one of these fast, cached based stores.
- array- セッションは、単にPHPの配列として保存されるだけです。リクエスト間で持続しません。- array- sessions will be stored in a simple PHP array and will not be persisted across requests.
**注目:**配列(array)ドライバーは、通常ユニットテストを実行するため使用されます。そのため、そのため、セッションデーターは保持されません。Note: The array driver is typically used for running unit tests[/docs/master/testing], so no session data will be persisted.