search
LoginSignup
20
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Laravel 5.3でセッション管理にredisを使用する方法

参照

設定方法

predis/predisパッケージをインストール

  • コンソールで下記を実行

    cd /your/project/path
    composer require predis/predis
    

redisサーバの設定を追加

  • config/database.phpの「Redis Databases」の情報を変更。
  • 実際には下記のようになっているので、.env.xxxxxファイルを変更。

    config/database.php
        /*
        |--------------------------------------------------------------------------
        | Redis Databases
        |--------------------------------------------------------------------------
        |
        | Redis is an open source, fast, and advanced key-value store that also
        | provides a richer set of commands than a typical key-value systems
        | such as APC or Memcached. Laravel makes it easy to dig right in.
        |
        */
    
        'redis'       => [
    
            'cluster' => false,
    
            'default' => [
                'host'     => env('REDIS_HOST', 'localhost'),
                'password' => env('REDIS_PASSWORD', null),
                'port'     => env('REDIS_PORT', 6379),
                'database' => 0,
            ],
    
        ],
    
  • .env.xxxxxを編集。

    env.xxxxx
    REDIS_HOST=(your_redis_host)
    REDIS_PASSWORD=(your_redis_password)
    REDIS_PORT=6379
    

セッションドライバーの変更

  • config/sessions.phpの「Default Session Driver」を変更
  • 実際には下記のようになっているので、.envファイルを変更。

    config/sessions.php
        /*
        |--------------------------------------------------------------------------
        | Default Session Driver
        |--------------------------------------------------------------------------
        |
        | This option controls the default session "driver" that will be used on
        | requests. By default, we will use the lightweight native driver but
        | you may specify any of the other wonderful drivers provided here.
        |
        | Supported: "file", "cookie", "database", "apc",
        |            "memcached", "redis", "array"
        |
        */
    
        'driver'          => env('SESSION_DRIVER', 'file'),
    
    
  • .env.xxxxxを編集。

    env.xxxxx
    SESSION_DRIVER=redis
    

余談

  • AP<->DB構成の場合、こんなことでハマってしまった…。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
20
Help us understand the problem. What are the problem?