页面之间的 ZF2 空会话容器 [英] ZF2 empty session container between pages

查看:76
本文介绍了页面之间的 ZF2 空会话容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网上浏览了几天后,我决定在这里寻求您的帮助.

After looking around the internet for days, i decided to ask your help here.

我在 Zend Framework 2 会话容器管理方面遇到了问题.我不明白为什么,但是每次我更改页面时,框架都会清空我的所有容器.

I got a problem with Zend Framework 2 session container management. I don't understand why, but the framework emptied all my containers each time i'm changing page.

我的情况很简单,我正在建立一个网上商店:

My case is simple, i'm building an online shop :

  • 客户正在购买产品并点击添加到购物车"按钮
  • 产品已保存到会话
  • 客户返回产品列表选择其他产品......但他的购物车中不再有产品.

这是一段代码:

// Create container to add product
$container = new Zend\Session\Container('frontCart');

// Add product to cart
$container->offsetSet('frontCartContent',
                      array(1 => serialize($my_product_object));

如果我在添加后立即调试会话:

If i make a debug of the session just after added :

Debug::dump($_SESSION);

// Display this :
["frontCart"] => object(Zend\Stdlib\ArrayObject)#70 (4) {
   ["storage":protected] => array(1) {
      ["frontCartContent"] => array(1) {
         [1] => string(1175) "my serialized product object"
      }
   }
   ["flag":protected] => int(2)
   ["iteratorClass":protected] => string(13) "ArrayIterator"
   ["protectedProperties":protected] => NULL
}

然后,如果我只是重新加载页面,或者从以下位置切换:

Then, if i simply reload the page, or if switch from :

http://mydomain.com/products_list/my_product

http://mydomain.com/products_list

我明白了:

Debug::dump($_SESSION);

// Display this :
["frontCart"] => NULL

请帮忙:-(我完全不明白为什么ZF2会有这种行为,如果不能添加和副产品,这对于网店客户来说是非常有问题的.

Please, help :-( I don't understand at all why ZF2 has this behavior, and this is very problematic for an online shop customer if he can't add and by products.

谢谢

编辑

按照 Tim 的要求,这里有更多代码.

Following Tim's demand here is more code.

我在控制器的构造函数中初始化我的会话容器

I initialize my session container in the controller's constructor

public function __construct()
{
    if (!$this->sessionCart)
    {
        $this->sessionCart = new Container(ConstantSession::FRONT_CART);
    }
}

然后,这是我将产品添加到容器中的确切方法

Then, here is the exact way i'm adding the product to the container

$this->sessionCart->offsetSet(ConstantSession::FRONT_CART_CONTENT,
                              array($cartNumber => serialize($product))
);

$cartNumber 会随着购物车中产品的数量增加(当它起作用时).$product 是一个具有所有属性的对象.

$cartNumber is incremented following the number of products in the cart (when it'll work). $product is an object with all its properties.

编辑 2

按照 Tim 的建议,我将添加到购物车"代码更改为:

Following Tim's advises i changed my "add to cart" code to :

$this->sessionCart->frontCartContent = array($cartNumber => $product);

当我想取回我的会话内容时,我创建了一个新的 Container 实例:

When i want to get back my session content i create a new instance of Container :

// Init new container
$container = new Zend\Session\Container('frontCart');

// Get the content
$container->frontCartContent;

如果我在最后一行做一个 Debug::dump(),在改变页面后我仍然得到 NULL.

If i make a Debug::dump() of the last line, i still get NULL after changing page.

推荐答案

您的代码存在一些问题.试试:

There are a few issues with your code. Try:

// Create container to add product
$container = new Zend\Session\Container('cart');

// Add product to cart
$container->frontCartContent = array($my_product_object);

然后在另一个页面上,您需要使用上面使用的相同参数再次创建容器,然后检查内容.不要只是调用 $_SESSION:

then on the other page, you need to create the container again with the same parameter you used above, and then check the contents. Don't just called $_SESSION:

$container = new Zend\Session\Container('cart');
var_dump($container->frontCartContent);

看看这是否会给您带来更好的结果.

See if that gives you better results.

这篇关于页面之间的 ZF2 空会话容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆