1

我有这样的代码:

        $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);

效果很好,但此代码在文件中的onBootstrap()方法中。Module.php有没有更好的方法(地点?)来实现会话?控制器插件是用于控制器的,那么这些是什么?

4

1 回答 1

4

我的建议是使它成为一个专用的低级模块。您可以将完整的配置和实例化封装到一个简单的模块中,您可以依赖于您的进一步应用程序。

这与我们处理邮件日志记录缓存完全相同(尽管缓存尚未完成)。在这些情况下,我们创建可以注入应用程序服务的服务。在您的情况下,我会将其设置为一个侦听器(是否封装在专用类中),您可以在onBootstrap()方法中对其进行初始化。

一个小例子:

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'
            ),
        );
    }
}

您将会话管理器的工厂逻辑封装在工厂类中:

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;
    }
}
于 2012-12-24T21:08:31.270 回答