Zend Framework 2实施会话的最佳方法 [英] Zend Framework 2 Best way to implement sessions

查看:85
本文介绍了Zend Framework 2实施会话的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

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

效果很好,但是此代码在Module.php文件的onBootstrap()方法中.有没有更好的方法(位置?)来实现会话? Controller插件用于Controller,那么这些又是什么呢?

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?

推荐答案

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

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.

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

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.

一个小例子:

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;
    }
}

这篇关于Zend Framework 2实施会话的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆