如何在使用ZF3和Doctrine时在数据库中实现会话?zf3与Doctrine的自定义会话SaveHandler
手册说:
There may be cases where you want to create a save handler where a save handler currently does not exist. Creating a custom save handler is much like creating a custom PHP save handler. All save handlers must implement Zend\Session\SaveHandler\SaveHandlerInterface. Generally if your save handler has options you will create another options class for configuration of the save handler.
我试图创建实现此接口的自定义类,但我得到了以下错误:
expects a class implementing Zend\Session\Storage\StorageInterface'
与此配置:
'session_storage' => [
// 'type' => SessionArrayStorage::class (with array storage works ok)
'type' => \Application\Session\SaveHandler\Doctrine::class (tried to implement suggested interface)
],
请注意,手册建议SaveHandlerInterface
,但预计StorageInterface
。
任何示例如何做到这一点?
编辑:
我目前的执行情况。
在global.php
:
'session_config' => [
// Session cookie will expire in 1 hour.
'cookie_lifetime' => 60*60*1,
// Session data will be stored on server maximum for 30 days.
'gc_maxlifetime' => 60*60*24*30,
],
// Session manager configuration.
'session_manager' => [
// Session validators (used for security).
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
]
],
// Session storage configuration.
'session_storage' => [
'type' => \Application\Session\Storage\Doctrine::class,
],
'session_containers' => [
'UserSession'
]
在Module.php
:
/**
* This method is called once the MVC bootstrapping is complete.
*/
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$serviceManager = $application->getServiceManager();
// The following line instantiates the SessionManager and automatically
// makes the SessionManager the 'default' one
/** @var SessionManager $sessionManager */
$sessionManager = $serviceManager->get(SessionManager::class);
$entityManager = $serviceManager->get('doctrine.entitymanager.orm_default');
/** @var Doctrine $storage */
$storage = $sessionManager->getStorage();
$storage->setEntityManager($
}
在Application\Session\Storage\Doctrine.php
:
class Doctrine implements
IteratorAggregate,
StorageInterface,
StorageInitializationInterface
{
public function setEntityManager($em) {
$this->entityManager = $em;
}
// ...
// other functions as required by interfaces
}
这个方法奏效,但缺点是教义存储只会在可这个模块,我专门在每个re上注入它任务(Boostrap),而不是当它真的需要时(工厂)。
**更新:**
我写SaveHandler
,不过貌似看重的请求都不会保留。
下面的代码:
class Doctrine extends ArrayStorage implements SaveHandlerInterface {
/**
* @param string $session_id
* @return string Encdoded session data string
*/
public function read($session_id)
{
$entity = $this->getEntity($session_id);
if ($entity) {
return $entity->getSessionData();
// sample output:
// string '__ZF|a:2:{s:20:"_REQUEST_ACCESS_TIME";d:1501933765.497678;s:6:"_VALID";a:3:{s:25:"Zend\Session\Validator\Id";s:26:"3kr15rhi6ijhneu7rruro9gr76";s:33:"Zend\Session\Validator\RemoteAddr";s:9:"127.0.0.1";s:36:"Zend\Session\Validator\HttpUserAgent";s:133:"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36";}}FlashMessenger|C:23:"Zend\Stdlib\ArrayObject":205:{a:4:{s:7:"storage";a:0:{}s:4:"flag";i:2;s:13:"iteratorClass";s:13:"ArrayI'... (length=645)
// note that counter is not present
}
}
/**
* @param string $session_id
* @param string $session_data Encoded session data string
* @return bool
*/
public function write($session_id, $session_data)
{
// sample input ($session_data):
// string '__ZF|a:2:{s:20:"_REQUEST_ACCESS_TIME";d:1501934933.9573331;s:6:"_VALID";a:3:{s:25:"Zend\Session\Validator\Id";s:26:"3kr15rhi6ijhneu7rruro9gr76";s:33:"Zend\Session\Validator\RemoteAddr";s:9:"127.0.0.1";s:36:"Zend\Session\Validator\HttpUserAgent";s:133:"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36";}}UserSession|C:23:"Zend\Stdlib\ArrayObject":223:{a:4:{s:7:"storage";a:1:{s:7:"counter";i:1;}s:4:"flag";i:2;s:13:"iteratorCla'... (length=918)
// (note that counter variable is set)
$entity = $this->getEntity($session_id);
$entity->setSessionData($session_data);
$entity->setLifetime($this->getLifeTime());
$this->getEntityManager()->persist($entity);
$this->getEntityManager()->flush($entity);
return true;
}
/**
* @param string $session_id
* @return Entity|null
*/
public function getEntity($session_id)
{
$this->entity = $this->getRepository()->find($session_id);
if (!$this->entity) {
$this->entity = new $this->entityName;
$this->entity->setId($session_id);
}
return $this->entity;
}
// ....
}
当你到底得到这个错误? – akond
我认为会话服务在会话管理器中注册时。 – takeshin
我写了一个非常简单的POC片断,其中包含了一些简单的SaveHandlerInterface实现。到目前为止没有错误。 – akond