Search code examples
zend-framework2zfcuser

How to share zfcuser across subdomain in zend framework 2


I have in my application installed zfcuser module and everything works fine. I configured hostname router and here my problem starts, when I log in on main domain (http://example.com) everything is ok, but when I go to any subdomain (http://test.example.com, http://anysubdomain.example.com) I'm loosing logged state and on every subdomain I have to login again. How to share login state across subdomains? In ZF1 i just set 'cookie_domain' and it works but how make it in ZF2? Of course I'm using also Bjyauthorize and I want to keep bjyauthorize guards on subdomains...


Solution

  • Ok I found solution, in ZfcUser Module.php I've added:

    use Zend\Session\Config\SessionConfig;
    use Zend\Session\SessionManager;
    use Zend\Session\Container;
    use Zend\EventManager\EventInterface;
    
    public function onBootstrap(EventInterface $e)
    {
       $config = $e->getApplication()
                  ->getServiceManager()
                  ->get('Configuration');
    
       $sessionConfig = new SessionConfig();
       $sessionConfig->setOptions($config['session']);
       $sessionManager = new SessionManager($sessionConfig);
       $sessionManager->start();
    
       Container::setDefaultManager($sessionManager);
    }
    

    and in ZfcUser module.config.php:

    return array(
      'session' => array(
        'use_cookies' => true,
        'cookie_domain'=>'example.com',
      )
    );
    

    Hope it will help somebody.