How to use Zend 2 save handler DbTableGateway?

后端 未结 2 405
孤街浪徒
孤街浪徒 2021-01-23 08:58

The Zend\\Session Save Handler tutorial gives an example for DbTableGateway in which they create a TableGateway with an undefined $adapter variable. I want to use the handler to

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-23 09:48

    The answer turned out to be like newage's answer:

    Since newage edited his answer to include the db adapter, I've accepted it as the right answer. The rest of this is just my implementation:

    You can remove all the TableGateway and savehandler logic from the bootstrapSession method and put it in the getServiceConfig method.

    Add the definition for Adapter to the 'factories' array in getServiceConfig, then modify the 'Zend\Session\SessionManager' function to include the Adapter, TableGateway, and save handler. This is what the new getServiceConfig would look like:

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
    
                // New code here
                'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
                // New code here
    
                'Zend\Session\SessionManager' => function ($sm) {
                    $config = $sm->get('config');
                    if (isset($config['session'])) {
                        $session = $config['session'];
    
                        $sessionConfig = null;
                        if (isset($session['config'])) {
                            $class = isset($session['config']['class'])  ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
                            $options = isset($session['config']['options']) ? $session['config']['options'] : array();
                            $sessionConfig = new $class();
                            $sessionConfig->setOptions($options);
                        }
    
                        $sessionStorage = null;
                        if (isset($session['storage'])) {
                            $class = $session['storage'];
                            $sessionStorage = new $class();
                        }
    
                        $sessionSaveHandler = null;
                        if (isset($session['save_handler'])) {
                            // class should be fetched from service manager since it will require constructor arguments
                            $sessionSaveHandler = $sm->get($session['save_handler']);
                        }
                        $sessionManager = new SessionManager();
                    }
    
                    // New code here
                    /* @var $adapter \Zend\Db\Adapter\Adapter */
                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $tableGateway = new TableGateway('mytablename', $adapter);
                    $saveHandler  = new DbTableGateway($tableGateway, new DbTableGatewayOptions());
                    $sessionManager->setSaveHandler($saveHandler);
                    // New code here
    
                    Container::setDefaultManager($sessionManager);
                    return $sessionManager;
                },
            ),
        );
    }
    

    Then add the database connection info to the module's config file:

    return array(
        // ...
        'db' => array(
            'driver'         => 'Pdo',
            'dsn'            => 'mysql:dbname=mydbname;host=mydbhost;port=xxxx',
            'driver_options' => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
            ),
            'username'       => 'dbusername',
            'password'       => 'dbpassword',
        ),
    );
    

提交回复
热议问题