我正在扩展会话提供程序,以便保留一些所需的数据。我开始编辑AppServiceProvider's boot method
:
'Session::extend('desk', function($app)
{
return new Desk();
});
Desk
类看起来像:
namespace App'Services;
use Illuminate'Session'ExistenceAwareInterface;
class Desk implements 'SessionHandlerInterface, ExistenceAwareInterface{
/**
* The existence state of the session.
* @var bool
*/
protected $exists;
public function close()
{
return true;
}
public function destroy($session_id)
{
$session = $em->find('Session', $session_id);
$em->remove($session);
$em->flush();
return true;
}
public function gc($maxlifetime)
{
// TODO: Implement gc() method.
}
public function open($save_path, $session_id)
{
return true;
}
public function read($session_id)
{
$session = $em->find('Session', $session_id);
if ($sesion !== null){
$this->exists = true;
return $session->getPayload();
}
}
public function write($session_id, $session_data)
{
$session = $em->find('Session', $session_id);
if ($session === null){
$session = new Session($session_id, $session_data);
$em->persist($session);
}
else{
$session->setPayload($session_data);
}
$em->flush();
$this->exists = true;
}
public function setExists($value)
{
$this->exists = $value;
return $this;
}
}
完成实现后,我将会话配置更改为:
return [
'driver' => 'desk',
'lifetime' => 120,
'expire_on_close' => false,
'encrypt' => false,
'files' => storage_path().'/framework/sessions',
'connection' => null,
'table' => 'sessions',
'lottery' => [2, 100],
'cookie' => 'lote_session',
'path' => '/',
'domain' => null,
'secure' => false,
];
当我加载页面时,没有问题,但在成功登录请求后,刷新页面,会话到期,用户再次成为访客。我错过什么了吗?
附加信息:如果我将会话驱动程序恢复为"文件",一切都会好起来。
好吧,对于像我这样需要/想要扩展会话提供程序的其他人,请注意会话的表结构。我的错误是payload
列被设置为:
payload varchar(255) not null
由于laravel序列化数据,有效负载值的长度可能超过255个字符,因此,它会破坏数据并使其不一致。您可以考虑:
payload text not null