問題描述
我正在其中一個中間件(用于 Laravel Web 應(yīng)用程序)中覆蓋 session.timeout
值,但它似乎不會影響會話超時.雖然如果我調(diào)試它會顯示我已經(jīng)覆蓋的值.
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);
默認(rèn)值如下:
'lifetime' => 15,
我正在開發(fā)的網(wǎng)站對大多數(shù)用戶的會話生命周期非常短,但對于選定的用戶,我想提供更長的會話生命周期.
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.
推薦答案
似乎實現(xiàn)動態(tài) lifetime
值的唯一方法是在會話啟動之前在中間件中設(shè)置該值.否則為時已??晚,因為應(yīng)用程序 SessionHandler 已經(jīng)使用默認(rèn)配置值進(jìn)行了實例化.
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 AppHttpMiddleware;
class ExtendSession
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $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
.
AppHttpMiddlewareExtendSession::class,
IlluminateSessionMiddlewareStartSession::class,
這篇關(guān)于Laravel 在用戶級別自定義 session.lifetime的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!