我正在尝试建立一个ZendFramework3MVC web应用程序来使用会话存储。以下是本网站提供的资料-
一切都很好。我在控制器中获得了会话变量,并且可以将数据保存到会话容器中。问题是,在随后的调用中,我保存到容器中的数据并不存在。我从一个页面中保存搜索条件,并将其重定向到另一个页面,以执行搜索并返回结果。当我输入第二页时,会话数据不存在。
在config\global.php我有--
return [
'session_config' => [
// Cookie expires in 1 hour
'cookie_lifetime' => 60*60*1,
// Stored on server for 30 days
'gc_maxlifetime' => 60*60*24*30,
],
'session_manager' => [
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
],
],
'session_storage' => [
'type' => SessionArrayStorage::class,
],
];在application\module.php中,我修改了onBoostrap
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$svcMgr = $application->getServiceManager();
// Instantiate the session manager and
// make it the default one
//
$sessionManager = $svcMgr->get(SessionManager::class);
}我创建了一个IndexControllerFactory
class IndexControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container,
$requestedName, array $options = null)
{
// Get access to session data
//
$sessionContainer = $container->get('Books\Session');
return new IndexController($sessionContainer);
}
}修改我的IndexController以添加构造函数方法
class IndexController extends AbstractActionController
{
private $session;
public function __construct(Container $session)
{
$this->session = $session;
}在application\module.config.php里我有这个
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
],
],
'session_containers' => [
'Books\Session'
],发布于 2016-12-21 11:54:03
要在会话中存储某些内容,您可以如下所示创建容器:
// Create a session container
$container = new Container('Books\Session');
$container->key = $value;要在以后从会话容器中检索某些内容,您必须创建一个同名的新容器:
// Retrieve from session container
$container = new Container('Books\Session');
$value = $container->key;据我所知,这同样适用于ZF2和ZF3,并且可以在other posts on StackOverflow或例如Using Sessions in Zend Framework 2中找到。
如果您创建一个新的Container来存储或解析会话中的数据,如果您不亲自传递数据,它将自动使用默认的会话管理器。
你可以看到那个 method on line 77。如果传递给构造函数的$manager是null,那么它将在 method中获得默认的会话管理器。
因此,要使用会话,您不需要做很多手动配置。
如果这不能解决你的问题,请留下评论。
https://stackoverflow.com/questions/41048799
复制相似问题