Laravel在用户级别自定义session.lifetime [英] Laravel customized session.lifetime at user level
问题描述
我正在其中一种中间件(用于Laravel Web应用程序)中覆盖session.timeout
的值,但是在超时会话方面似乎没有影响.尽管如果我调试它会显示出我已经覆盖的值.
I am overwriting session.timeout
value in one of the middleware (for Laravel web app) but it doesn't seem to be affecting in terms of timing out a session. Though if I debug it shows value I have overwritten.
Config::set('session.lifetime', 1440);
默认值如下:
'lifetime' => 15,
我正在工作的网站的大多数用户的会话生存期非常短,但对于某些用户,我希望提供更长的会话生存期.
Website that I am working on has very short session lifetime for most of the users but for selected users I want to provide extended session lifetime.
推荐答案
看来,完成动态lifetime
值的唯一方法是在启动会话之前在中间件中设置该值.否则为时已晚,因为已经使用默认配置值实例化了应用程序SessionHandler.
It seems the only way to accomplish a dynamic lifetime
value, is by setting the value in middleware, before the session gets initiated. Otherwise its too late, as the application SessionHandler will have already been instantiated using the default config value.
namespace App\Http\Middleware;
class ExtendSession
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, $next)
{
$lifetime = 2;
config(['session.lifetime' => $lifetime]);
return $next($request);
}
}
然后在kernel.php文件中,在StartSession
之前添加此类.
Then in the kernel.php file, add this class prior to StartSession
.
\App\Http\Middleware\ExtendSession::class,
\Illuminate\Session\Middleware\StartSession::class,
这篇关于Laravel在用户级别自定义session.lifetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!