ZF3とDoctrineを使用しているときにデータベースでセッションを実装する方法は?Doctrineを使用したzf3のカスタムセッション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に注入しますクエスト(ブーストラップ)、本当に必要なときではない(ファクトリ)。
**アップデート:**
私は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
簡単なSaveHandlerInterface実装で非常に単純なPOCスニペットを作成しました。これまでのところエラーはありません。 – akond