简体   繁体   English

页面之间的ZF2空会话容器

[英]ZF2 empty session container between pages

After looking around the internet for days, i decided to ask your help here. 在互联网上浏览了几天后,我决定在这里向您寻求帮助。

I got a problem with Zend Framework 2 session container management. Zend Framework 2会话容器管理出现问题。 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 : 我的情况很简单,我正在建立一家网上商店:

  • The customer is on a product and click the "add to cart" button 客户正在使用某种产品,然后单击“添加到购物车”按钮
  • The product is saved to session 产品已保存到会话
  • The customer get back to the products list to choose another product ... but there is no product anymore in his cart. 客户返回到产品列表以选择其他产品...,但是他的购物车中没有产品了。

Here is a piece of code : 这是一段代码:

// 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/my_product

to

http://mydomain.com/products_list http://mydomain.com/products_list

I get : 我得到:

Debug::dump($_SESSION);

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

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. 请帮忙:-(我根本不理解为什么ZF2会出现这种行为,这对于在线商店的客户来说非常麻烦,如果他不能添加产品和添加副产品。

Thx 谢谢

EDIT 编辑

Following Tim's demand here is more code. 遵循Tim的要求,这里有更多代码。

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 is incremented following the number of products in the cart (when it'll work). $ cartNumber随购物车中的产品数量增加(有效时)。 $product is an object with all its properties. $ product是具有所有属性的对象。

EDIT 2 编辑2

Following Tim's advises i changed my "add to cart" code to : 按照Tim的建议,我将“添加到购物车”代码更改为:

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

When i want to get back my session content i create a new instance of Container : 当我想恢复会话内容时,我创建了一个Container的新实例:

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

// Get the content
$container->frontCartContent;

If i make a Debug::dump() of the last line, i still get NULL after changing page. 如果我在最后一行执行Debug :: dump(),则在更改页面后仍会得到NULL。

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);

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 : 不要只叫$_SESSION

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

See if that gives you better results. 看看是否能为您带来更好的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM