【发布时间】:2015-06-21 00:32:48
【问题描述】:
我在 StackOverflow 上看到了很多问题,我有这段代码
use Zend\Session\Container;
class IndexController extends AbstractActionController {
public function indexAction() {
$userSession = new Container('user');
$userSession->username = 'Sandhya';
return new ViewModel();
}
}
当我在控制器中打印$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
)
)
这意味着没有像username...
但是当我打印 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
)
)
)
有一个字段username...
但是当我尝试查看 $_SESSION 时,它给了我与上面相同的输出..
问题是我无法在容器和$_SESSION 中获得username。
我需要它在控制器中。
可能是什么问题需要帮助?谢谢。
【问题讨论】:
-
您希望此用户名出现在视图部分吗?
-
不,我想在控制器中使用这个
-
好的,我正在发布对您有帮助的答案。
-
会话容器只是
$_SESSION的包装器。 var_dump 容器时看不到您的用户名,因为它存储在会话中。如果您无法通过以下方式访问用户名。视图中的容器,请编辑您的问题以包含您用于测试的代码。 -
我正在控制器本身中获取会话的值并将这些值传递给查看 ....
标签: php session zend-framework2