Reputation: 2928
I retrieve data from Zend 2 session container on layout via bootstarp method.
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$sm = $e->getApplication()->getServiceManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$user = $this->getUser($sm);
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->user = ($user) ? $user: false;
}
public function getUser($sm)
{
$user= false;
$userTable= $sm->get('User\Model\UserTable');
$userSession = new Container('userSession');
//RETRIEVE USER FROM DB
return $user;
}
Now i'm getting following error sometimes (not always)
Fatal error: Uncaught exception 'Zend\Stdlib\Exception\InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in /var/www/T2oRecruitment/app/vendor/zendframework/zendframework/library/Zend/Stdlib/ArrayObject.php:184 Stack trace: #0
/var/www/T2oRecruitment/app/vendor/zendframework/zendframework/library/Zend/Stdlib/ArrayObject.php(411): Zend\Stdlib\ArrayObject->exchangeArray(NULL) #1 [internal function]: Zend\Stdlib\ArrayObject->unserialize('a:4:{s:7:"stora...') #2
/var/www/T2oRecruitment/app/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php(95): session_start() #3
/var/www/T2oRecruitment/app/vendor/zendframework/zendframework/library/Zend/Session/AbstractContainer.php(78): Zend\Session\SessionManager->start() #4
/var/www/T2oRecruitment/app/module/Application/Module.php(22): Zend\Session\AbstractContainer->__construct('userSession') #5
/var/www/T2oRecruitment/app/module/Application/Module.php(43): Application\Module->getUser(Object(Zend\S in /var/www/T2oRecruitment/app/vendor/zendframework/zendframework/library/Zend/Stdlib/ArrayObject.php on line 184
What is the issue ?
Upvotes: 3
Views: 1941
Reputation: 4898
There is official bug https://github.com/zendframework/zf2/issues/4821 it's still open, but you can check the progress...
Upvotes: 0
Reputation: 3986
Please try with this:
In Module.php file --->
use Zend\Session\Config\SessionConfig, Zend\Session\SessionManager,
Zend\Session\Container, Zend\Mvc\MvcEvent;
// other libraries
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$sm = $e->getApplication()->getServiceManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$config = $sm->get('Config');
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config['session']);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->start();
$user = $this->userSession($sm);
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->user = ($user) ? $user: false;
}
In module.config.php file --->
return array(
'session' => array(
'remember_me_seconds' => 2419200,
'use_cookies' => true,
'cookie_httponly' => true
),
);
Hope, this will give some idea to you, for fixing your issue. Thanks :)
Upvotes: 1