Session

設定

由於 HTTP 協定是無狀態(Stateless)的,所以 session 提供一種儲存用戶資料的方法。Laravel 支援了多種 session 後端驅動,並透過清楚、統一的 API 提供使用。也內建支援像是 MemcachedRedis 和資料庫的後端驅動。

session 的設定檔配置在 config/session.php 中,請務必看一下 session 設定檔中可用的選項設定及註解。Laravel 預設使用 file 的 session 驅動,它在大多的應用中可以良好運作。

如果你想在 Laravel 中使用 Redis sessions,你需要先透過 Composer 安裝 predis/predis 套件 (~1.0)。

注意: 如果你需要加密所有的 session 資料,就將選項 encrypt 設定為 true

Note: When using the cookie session driver, you should never remove the EncryptCookie middleware from your HTTP kernel. If you remove this middleware, your application will be vulnerable to remote code injection.

保留鍵值

Laravel 框架在內部有使用 flash 作為 session 的鍵值,所以應該避免 session 使用此名稱。

使用 Session

The session may be accessed in several ways, via the HTTP request's session method, the Session facade, or the session helper function. When the session helper is called without arguments, it will return the entire session object. For example:

session()->regenerate();

儲存項目到 Session 中

Session::put('key', 'value');

session(['key' => 'value']);

儲存項目進 Session 陣列值中

Session::push('user.teams', 'developers');

從 Session 取回項目

$value = Session::get('key');

$value = session('key');

從 Session 取回項目,若無則回傳預設值

$value = Session::get('key', 'default');

$value = Session::get('key', function() { return 'default'; });

從 Session 取回項目,並刪除

$value = Session::pull('key', 'default');

從 Session 取出所有項目

$data = Session::all();

判斷項目在 Session 中是否存在

if (Session::has('users'))
{
    //
}

從 Session 中移除項目

Session::forget('key');

清空所有 Session

Session::flush();

重新產生 Session ID

Session::regenerate();

暫存資料(Flash Data)

有時你可能希望暫存一些資料,並只在下次請求有效。你可以使用 Session::flash 方法來達成目的:

Session::flash('key', 'value');

刷新當前暫存資料,延長到下次請求

Session::reflash();

只刷新指定快閃資料

Session::keep(['username', 'email']);

資料庫 Sessions

當使用 database session 驅動時,你必需建置一張儲存 session 的資料表。下方範例使用 Schema 來建表:

Schema::create('sessions', function($table)
{
    $table->string('id')->unique();
    $table->text('payload');
    $table->integer('last_activity');
});

當然你也可以使用 Artisan 指令 session:table 來建 migration 表:

php artisan session:table

composer dump-autoload

php artisan migrate

Session 驅動

session 設定檔中的「driver」定義了 session 資料將以哪種方式被儲存。Laravel 提供了許多良好的驅動:

注意: array 驅動典型應用在 unit tests 環境下,所以不會留下任何 session 資料。