[英]Session container not working in zf2?
I have seen many questions on StackOverflow and I have this code 我在StackOverflow上看到很多问题,我有这个代码
use Zend\Session\Container;
class IndexController extends AbstractActionController {
public function indexAction() {
$userSession = new Container('user');
$userSession->username = 'Sandhya';
return new ViewModel();
}
}
When I am printing the $userSession
container in the controller it is giving me this output 当我在控制器中打印
$userSession
容器时,它给我这个输出
Zend\Session\Container Object (
[name:protected] => user
[manager:protected] => Zend\Session\SessionManager Object (
[defaultDestroyOptions:protected] => Array (
[send_expire_cookie] => 1
[clear_storage] =>
)
[name:protected] =>
[validatorChain:protected] =>
[config:protected] => Zend\Session\Config\SessionConfig Object (
[phpErrorCode:protected] =>
[phpErrorMessage:protected] =>
[rememberMeSeconds:protected] => 240
[serializeHandler:protected] =>
[validCacheLimiters:protected] => Array (
[0] => nocache
[1] => public
[2] => private
[3] => private_no_expire
)
[validHashBitsPerCharacters:protected] => Array (
[0] => 4
[1] => 5
[2] => 6
)
[validHashFunctions:protected] =>
[name:protected] =>
[savePath:protected] =>
[cookieLifetime:protected] => 2592000
[cookiePath:protected] =>
[cookieDomain:protected] =>
[cookieSecure:protected] =>
[cookieHttpOnly:protected] => 1
[useCookies:protected] => 1
[options:protected] => Array (
[gc_maxlifetime] => 2592000
)
)
[defaultConfigClass:protected] => Zend\Session\Config\SessionConfig
[storage:protected] => Zend\Session\Storage\SessionArrayStorage Object (
)
[defaultStorageClass:protected] => Zend\Session\Storage\SessionArrayStorage
[saveHandler:protected] =>
)
[storage:protected] => Array ( )
[flag:protected] => 2
[iteratorClass:protected] => ArrayIterator
[protectedProperties:protected] => Array (
[0] => name
[1] => manager
[2] => storage
[3] => flag
[4] => iteratorClass
[5] => protectedProperties
)
)
It means there is nothing like username
... 这意味着没有像
username
...
But when I am printing the S_SESSION
it gives me this output... 但是当我打印
S_SESSION
它给了我这个输出......
Array (
[__ZF] => Array (
[_REQUEST_ACCESS_TIME] => 1429081041.81
)
[user] => Zend\Stdlib\ArrayObject Object (
[storage:protected] => Array (
[username] => Sandhya
)
[flag:protected] => 2
[iteratorClass:protected] => ArrayIterator
[protectedProperties:protected] => Array (
[0] => storage
[1] => flag
[2] => iteratorClass
[3] => protectedProperties
)
)
)
There is a field username
... 有一个字段
username
...
But when I am trying to get the $_SESSION
in view it gives me the same output as above.. 但是当我试图在视图中获得
$_SESSION
,它给出了与上面相同的输出。
The problem is I am not able to get the username
in both the container as well as in $_SESSION
. 问题是我无法在容器和
$_SESSION
获取username
。 I need it in the controllers. 我需要它在控制器中。 what can be the problem need help?
可能有什么问题需要帮助? Thank you.
谢谢。
I think you have to work on your configuration. 我认为你必须处理你的配置。 You have to setup a common
SessionManager
to manage handling of your session information. 您必须设置一个通用的
SessionManager
来管理会话信息的处理。
Something like this: 像这样的东西:
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->start();
Container::setDefaultManager($sessionManager);
I would suggest registering your SessionManager config in your ServiceManager instance and then use it throughout the application. 我建议在ServiceManager实例中注册SessionManager配置,然后在整个应用程序中使用它。
'service_manager' => array(
'factories' => array(
'session_manager' => 'My\Factory\SessionManagerFactory'
)
)
You can then get your SessionManager in any controller: 然后,您可以在任何控制器中获取SessionManager:
$sessionManager = $this->serviceLocator->get('session_manager');
And if you create a new Container
it will use your common/default SessionManager
instance automatically so all will be managed in one place. 如果您创建一个新的
Container
它将自动使用您的公共/默认SessionManager
实例,因此所有将在一个地方进行管理。
$userSession = new Container('user');
$userSession->getManager() === $this->serviceLocator->get('session_manager') // true
On how to register your session_manager I will refer to the official ZF2 documentation . 关于如何注册session_manager,我将参考官方的ZF2文档 。
You can use the following code: 您可以使用以下代码:
$userSession = new Container('user');
//To check the session variable in zf2:
if($userSession->offsetExists('username')){
//Your logic after check condition
}
This will return true or false on the basis of session exist or not. 这将根据会话是否存在返回true或false。
//To get the value of session:
echo $user->offsetGet('username');
Above code will return the value of session index username
. 上面的代码将返回会话索引
username
的值。
Instead of $userSession->username = 'Sandhya';
而不是
$userSession->username = 'Sandhya';
you can use below code: 你可以使用下面的代码:
$user->offsetSet('username','Sandhya');
This is zf2 standard, which is used by session container in zf2. 这是zf2标准,由zf2中的会话容器使用。
you can just get your username from session in controllers. 你可以从控制器中的会话中获取用户名。
$userSession = new Container('user');
$username = $userSession->username ;
var_dump($username); //Sandhya
it work for me . 它对我有用。 try it !
试试吧 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.