dongshi6710 2012-12-24 19:57
浏览 68
已采纳

Zend Framework 2实现会话的最佳方式

I have a code like this:

        $sessionOptions = new SessionDbSavehandlerOptions();
        $sessionOptions->setDataColumn('data')
                       ->setIdColumn('id')
                       ->setModifiedColumn('modified')
                       ->setLifetimeColumn('lifetime')
                       ->setNameColumn('name');
        $application    = $event->getApplication();
        $serviceManager = $application->getServiceManager();
        $dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
        $sessionTableGateway = new TableGateway('zf2_sessions', $dbAdapter);
        $sessionGateway = new DbTableGateway($sessionTableGateway, $sessionOptions);
        $config = $serviceManager->get('Configuration');
        $sessionConfig = new SessionConfig();
        $sessionConfig->setOptions($config['session']);
        $sessionManager = new SessionManager($sessionConfig);
        $sessionManager->setSaveHandler($sessionGateway);
        $sessionManager->start();
        Container::setDefaultManager($sessionManager);

works good but this code is in onBootstrap() method in Module.php file. Is there a better way (place?) to implement session? Controller plugins are for Controller, so what is for these?

  • 写回答

1条回答 默认 最新

  • douwu7563 2012-12-24 21:08
    关注

    My suggestion would be to make this a dedicated, low level module. You can encapsulate the complete configuration and instantiation into a simple module which you can depend on for your further application.

    It is quite the same as we handle our mail, logging and cache (though cache is not complete yet). In those cases we create services which we can inject in our application services. In your case, I would make it a listener (encapsuled in a dedicated class or not) where you initialize it in your onBootstrap() method.

    A small example:

    namespace MySession;
    
    use Zend\Session\Container;
    
    class Module
    {
        public function onBootstrap($e)
        {
            $app = $e->getApplication();
            $sm  = $app->getServiceManager();
    
            $manager = $sm->get('session_manager');
            $manager->start();
    
            Container::setDefaultManager($manager);
        }
    
        public function getServiceConfig()
        {
            return array(
                'factories' => array(
                    'session_manager' => 'MySession\Service\SessionManagerFactory'
                ),
            );
        }
    }
    

    And you encapsulate the session manager's factory logic in a factory class:

    namespace MySession\Service;
    
    use Zend\ServiceManger\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;
    
    // Your imports further here
    
    class SessionManagerFactory implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $sl)
        {
            $sessionOptions = new SessionDbSavehandlerOptions();
            $sessionOptions->setDataColumn('data')
                           ->setIdColumn('id')
                           ->setModifiedColumn('modified')
                           ->setLifetimeColumn('lifetime')
                           ->setNameColumn('name');
            $application    = $event->getApplication();
            $serviceManager = $application->getServiceManager();
            $dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
            $sessionTableGateway = new TableGateway('zf2_sessions', $dbAdapter);
            $sessionGateway = new DbTableGateway($sessionTableGateway, $sessionOptions);
            $config = $serviceManager->get('Configuration');
            $sessionConfig = new SessionConfig();
            $sessionConfig->setOptions($config['session']);
            $sessionManager = new SessionManager($sessionConfig);
            $sessionManager->setSaveHandler($sessionGateway);
    
            return $sessionManager;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题