Home >Backend Development >PHP Tutorial >关于Laravel Session使用自定义Driver的问题
我现在准备用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>