Zend Framework 2 Best way to implement sessions

可紊 提交于 2020-01-05 15:02:20

问题


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:


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


来源:https://stackoverflow.com/questions/14025252/zend-framework-2-best-way-to-implement-sessions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!