Laravel 5.0 Session

Configuration

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, Redis, and databases is included out of the box.

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.

Before using Redis sessions with Laravel, you will need to install the predis/predis package (~1.0) via Composer.

Note: If you need all stored session data to be encrypted, set the encrypt configuration option to 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.

Reserved Keys

The Laravel framework uses the flash session key internally, so you should not add an item to the session by that name.

Session Usage

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();

Storing An Item In The Session

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

session(['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');

$value = session('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();

Regenerating The Session ID

Session::regenerate();

Flash Data

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(['username', 'email']);

Database Sessions

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');
});

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

The session "driver" defines where session data will be stored for each request. Laravel ships with several great drivers out of the box:

Note: The array driver is typically used for running unit tests, so no session data will be persisted.

ドキュメント章別ページ

Artisan CLI

ヘッダー項目移動

注目:アイコン:ページ内リンク設置(リンクがないヘッダーへの移動では、リンクがある以前のヘッダーのハッシュをURLへ付加します。

移動

クリックで即時移動します。

設定

適用ボタンクリック後に、全項目まとめて適用されます。

カラーテーマ
和文指定 Pagination
和文指定 Scaffold
Largeスクリーン表示幅
インデント
本文フォント
コードフォント
フォント適用確認

フォントの指定フィールドから、フォーカスが外れると、当ブロックの内容に反映されます。EnglishのDisplayもPreviewしてください。

フォント設定時、表示に不具合が出た場合、当サイトのクッキーを削除してください。

バックスラッシュを含むインライン\Code\Blockの例です。

以下はコードブロックの例です。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * ユーザに関連する電話レコードを取得
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

設定を保存する前に、表示が乱れないか必ず確認してください。CSSによるフォントファミリー指定の知識がない場合は、フォントを変更しないほうが良いでしょう。

キーボード・ショートカット

オープン操作

PDC

ページ(章)移動の左オフキャンバスオープン

HA

ヘッダー移動モーダルオープン

MS

移動/設定の右オフキャンバスオープン

ヘッダー移動

T

最初のヘッダーへ移動

E

最後のヘッダーへ移動

NJ

次ヘッダー(H2〜H4)へ移動

BK

前ヘッダー(H2〜H4)へ移動

その他

?

このヘルプページ表示
閉じる