How to Bootstrap Sessions in Zend Framework 2

前端 未结 1 639
[愿得一人]
[愿得一人] 2020-12-28 22:01

What is the best way to go about getting sessions up and running in Zend Framework 2? I\'ve tried setting session_start() in my index.php file but then that get

相关标签:
1条回答
  • 2020-12-28 22:23

    If i understand you correctly, all you wanna do is have your session working properly in your modules? Assuming that's correct there are two single steps.

    1) Create the config: module.config.php

    return array(
        'session' => array(
            'remember_me_seconds' => 2419200,
            'use_cookies' => true,
            'cookie_httponly' => true,
        ),
    );
    

    2) Start your Session: Module.php

    use Zend\Session\Config\SessionConfig;
    use Zend\Session\SessionManager;
    use Zend\Session\Container;
    use Zend\EventManager\EventInterface;
    
    public function onBootstrap(EventInterface $evm)
    {
        $config = $evm->getApplication()
                      ->getServiceManager()
                      ->get('Configuration');
    
        $sessionConfig = new SessionConfig();
        $sessionConfig->setOptions($config['session']);
        $sessionManager = new SessionManager($sessionConfig);
        $sessionManager->start();
    
        /**
         * Optional: If you later want to use namespaces, you can already store the 
         * Manager in the shared (static) Container (=namespace) field
         */
        Container::setDefaultManager($sessionManager);
    }
    

    Find more options in the documentation of \Zend\Session\Config\SessionConfig

    If you want to store cookies too, then please see this Question. Credits to Andreas Linden for his initial answer - I'm simply copy pasting his.

    0 讨论(0)
提交回复
热议问题