Laravel Redis 会话驱动程序不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20375089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Redis Session Driver doesn't work
提问by Ernie
I'm having difficulties using the laravel 4 "redis" session driver. The problem is nothing gets actually stored.
我在使用 laravel 4“redis”会话驱动程序时遇到了困难。问题是没有实际存储任何内容。
Things i've checked:
我检查过的东西:
- I've set the driver to
redis
in Session-config file and cache-config file - In database-config file a database config for redis is specified:
I tried storing something manually with the redis-class, that did work, storing with the session class did not work
// this is in my session config 'driver' => 'redis', //this is my db-config 'redis' => array( 'cluster' => true, 'default' => array( 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, ), ), //this works $redis = Redis::connection(); $redis->set('name', 'Taylor'); //this doesn't work Session::put('name', 'Taylor');
- 我已经
redis
在 Session-config 文件和 cache-config 文件中设置了驱动程序 - 在 database-config 文件中指定了 redis 的数据库配置:
我尝试用 redis-class 手动存储一些东西,确实有效,用 session 类存储没有用
// this is in my session config 'driver' => 'redis', //this is my db-config 'redis' => array( 'cluster' => true, 'default' => array( 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, ), ), //this works $redis = Redis::connection(); $redis->set('name', 'Taylor'); //this doesn't work Session::put('name', 'Taylor');
When i change the session to native
it works
当我将会native
话更改为有效时
Any help would be appreciated
任何帮助,将不胜感激
UPDATE
That's interesting:
When I use redis-cli monitor
as suggested by @philo, I get some output while trying to log in my L4 application:
更新
这很有趣:当我redis-cli monitor
按照@philo 的建议使用时,我在尝试登录 L4 应用程序时得到一些输出:
`1387191809.513730 [0 127.0.0.1:59268] "SELECT" "0"
1387191809.513835 [0 127.0.0.1:59268] "GET" "laravel:siau639prmckja34le11vbsfl7"
1387191809.863851 [0 127.0.0.1:59268] "SET" "laravel:siau639prmckja34le11vbsfl7" "s:226:\"_sf2_attributes|a:2:{s:6:\"_token\";s:40:\"9LPf354C2ZNtw0Oc1zyafSvMdFFlspPiJsq8w90v\";s:5:\"flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:\"u\";i:1387191809;s:1:\"c\";i:1387191809;s:1:\"l\";s:1:\"0\";}\";"
1387191809.866655 [0 127.0.0.1:59268] "EXPIRE" "laravel:siau639prmckja34le11vbsfl7" "0"
1387191828.464840 [0 127.0.0.1:59279] "SELECT" "0"
1387191828.464970 [0 127.0.0.1:59279] "GET" "laravel:siau639prmckja34le11vbsfl7"
1387191828.581774 [0 127.0.0.1:59279] "SET" "laravel:siau639prmckja34le11vbsfl7" "s:599:\"_sf2_attributes|a:4:{s:6:\"_token\";s:40:\"KSkc5OAsp9Psz3MC7dYo6FfkTvcdK6I6HcisSyJ3\";s:10:\"_old_input\";a:3:{s:6:\"_token\";s:40:\"9LPf354C2ZNtw0Oc1zyafSvMdFFlspPiJsq8w90v\";s:8:\"username\";s:10:\"bertcasier\";s:8:\"password\";s:4:\"test\";}s:5:\"flash\";a:2:{s:3:\"new\";a:0:{}s:3:\"old\";a:2:{i:0;s:10:\"_old_input\";i:1;s:6:\"errors\";}}s:6:\"errors\";O:29:\"Illuminate\Support\MessageBag\":2:{s:11:\"\x00*\x00messages\";a:1:{s:7:\"general\";a:1:{i:0;s:48:\"Ongeldige gebruikersnaam/wachtwoordcombinatie...\";}}s:9:\"\x00*\x00format\";s:8:\":message\";}}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:\"u\";i:1387191828;s:1:\"c\";i:1387191828;s:1:\"l\";s:1:\"0\";}\";"
1387191828.582214 [0 127.0.0.1:59279] "EXPIRE" "laravel:siau639prmckja34le11vbsfl7" "0"
1387191828.613536 [0 127.0.0.1:59284] "SELECT" "0"
1387191828.613649 [0 127.0.0.1:59284] "GET" "laravel:siau639prmckja34le11vbsfl7"
1387191828.653734 [0 127.0.0.1:59284] "SET" "laravel:siau639prmckja34le11vbsfl7" "s:226:\"_sf2_attributes|a:2:{s:6:\"_token\";s:40:\"SMxXljPLDaViVVSpCohfOKlpByhjp8E2ywS6zVkh\";s:5:\"flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:\"u\";i:1387191828;s:1:\"c\";i:1387191828;s:1:\"l\";s:1:\"0\";}\";"
1387191828.654102 [0 127.0.0.1:59284] "EXPIRE" "laravel:siau639prmckja34le11vbsfl7" "0"
1387191878.208814 [0 127.0.0.1:59358] "config" "get" "databases"
1387191878.215094 [0 127.0.0.1:59358] "INFO" "keyspace"
1387191880.176125 [0 127.0.0.1:59358] "select" "0"
1387191880.176314 [0 127.0.0.1:59358] "keys" "*"`
Seems like my session expires immediately? I wrote a custom Auth User Provider which logs me in, in a rest-API, and stores the result in the session. But when I use the redis session driver, I get redirected immediately back to the login page. When I use RDM (redis client GUI) I can't find any session variables
好像我的会话会立即过期?我编写了一个自定义 Auth 用户提供程序,它将我登录到一个 rest-API 中,并将结果存储在会话中。但是当我使用 redis 会话驱动程序时,我会立即被重定向回登录页面。当我使用 RDM(redis 客户端 GUI)时,我找不到任何会话变量
采纳答案by Ernie
I've found the solution thanks to the tip from @philo.
感谢@philo 的提示,我找到了解决方案。
The session lifetime in my config file was set to zero
, because I want my session to expire on browser close. That works for native sessions, but for redis sessions it causes the session to expire immediately. When I change the life time, my session works as expected.
我的配置文件中的会话生存期设置为zero
,因为我希望我的会话在浏览器关闭时过期。这适用于本机会话,但对于 redis 会话,它会导致会话立即过期。当我改变生命周期时,我的会话按预期工作。
Now I'm still searching how to expire on browser close
现在我仍在搜索如何在浏览器关闭时过期
回答by mevdschee
Now I'm still searching how to expire on browser close
现在我仍在搜索如何在浏览器关闭时过期
Note that the lifetime of the cookie and the session data are unrelated. You want the cookie lifetime to be 0 (remove on browser close) and the session data lifetime to be longer (as long as you allow between requests).
请注意,cookie 的生命周期与会话数据无关。您希望 cookie 生命周期为 0(在浏览器关闭时删除)并且会话数据生命周期更长(只要您在请求之间允许)。
回答by mal
You have to enclose your route to controller with middlewareGroups - Laravel 5.2
您必须使用 middlewareGroups - Laravel 5.2 将您的路由包含在控制器中
Route::group(['middlewareGroups' => ['web']], function () { ... });
Route::group(['middlewareGroups' => ['web']], function () { ... });