Home >Backend Development >PHP Tutorial >关于Laravel Session使用自定义Driver的问题

关于Laravel Session使用自定义Driver的问题

WBOY
WBOYOriginal
2016-06-06 20:17:441510browse

我现在准备用Redis来替代原来的file-based session存储。
但是我跟着配置完之后laravel还是把session存储在文件里面.

<code>//这个文件是存放在app\Extensions目录下的.
<?php namespace App\Extensions;

//use Illuminate\Support\Facades\Redis as Redis;

class RedisSessionStore implements SessionHandlerInterface {

    public function open($savePath, $sessionName) {}

    public function close() {}

    public function read($sessionId) {
        //$redis = Redis::connection('default');
        //return $redis->get($sessionId);
        return Redis::get($sessionId);
    }

    public function write($sessionId, $data) {
        //$redis = Redis::connection('default');
        //$redis->set($sessionId, $data);
        Redis::set($sessionId, $data);
    }

    public function destroy($sessionId) {
        //$redis = Redis::connection('default');
        //$redis->command('del', $sessionId);
        Redis::command('del', $sessionId);
    }

    public function gc($lifetime) {
        //
    }
}</code>
<code>//然后这个文件是写在app\Providers目录下的
<?php namespace App\Providers;

use Session;
use App\Extensions\RedisSessionStore;
use Illuminate\Support\ServiceProvider;

class SessionServiceProvider extends ServiceProvider {

    public function boot() {
        Session::extend('redis', function($app) {
            return new RedisSessionStore;
        });
    }

    public function register() {}
}</code></code>

我在app\config\app.php里面加入了自己写的App\Providers\SessionServiceProvider::class,进去。
还有替换了app\config\session.php里面的'driver' => env('SESSION_DRIVER', 'redis'),

调用的时候应该直接$request->session()->set($sessionId, $data)就能够存储到Redis里边的吧?

回复内容:

我现在准备用Redis来替代原来的file-based session存储。
但是我跟着配置完之后laravel还是把session存储在文件里面.

<code>//这个文件是存放在app\Extensions目录下的.
<?php namespace App\Extensions;

//use Illuminate\Support\Facades\Redis as Redis;

class RedisSessionStore implements SessionHandlerInterface {

    public function open($savePath, $sessionName) {}

    public function close() {}

    public function read($sessionId) {
        //$redis = Redis::connection('default');
        //return $redis->get($sessionId);
        return Redis::get($sessionId);
    }

    public function write($sessionId, $data) {
        //$redis = Redis::connection('default');
        //$redis->set($sessionId, $data);
        Redis::set($sessionId, $data);
    }

    public function destroy($sessionId) {
        //$redis = Redis::connection('default');
        //$redis->command('del', $sessionId);
        Redis::command('del', $sessionId);
    }

    public function gc($lifetime) {
        //
    }
}</code>
<code>//然后这个文件是写在app\Providers目录下的
<?php namespace App\Providers;

use Session;
use App\Extensions\RedisSessionStore;
use Illuminate\Support\ServiceProvider;

class SessionServiceProvider extends ServiceProvider {

    public function boot() {
        Session::extend('redis', function($app) {
            return new RedisSessionStore;
        });
    }

    public function register() {}
}</code></code>

我在app\config\app.php里面加入了自己写的App\Providers\SessionServiceProvider::class,进去。
还有替换了app\config\session.php里面的'driver' => env('SESSION_DRIVER', 'redis'),

调用的时候应该直接$request->session()->set($sessionId, $data)就能够存储到Redis里边的吧?

题主应该没有更新.env里面关于session的配置,请修改.env以下项:

<code>SESSION_DRIVER=redis</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn