title: ZF2 empty session container between pages
body: <p>After looking around the internet for days, i decided to ask your help here.</p>
  
  <p>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.</p>
  
  <p>My case is simple, i'm building an online shop :</p>
  
  <ul>
  <li>The customer is on a product and click the "add to cart" button</li>
  <li>The product is saved to session</li>
  <li>The customer get back to the products list to choose another product ... but there is no product anymore in his cart.</li>
  </ul>
  
  <p>Here is a piece of code :</p>
  
  <pre><code>// Create container to add product
  $container = new Zend\Session\Container('frontCart');
  
  // Add product to cart
  $container-&gt;offsetSet('frontCartContent',
                        array(1 =&gt; serialize($my_product_object));
  </code></pre>
  
  <p>If i make a debug of the session just after added :</p>
  
  <pre><code>Debug::dump($_SESSION);
  
  // Display this :
  ["frontCart"] =&gt; object(Zend\Stdlib\ArrayObject)#70 (4) {
     ["storage":protected] =&gt; array(1) {
        ["frontCartContent"] =&gt; array(1) {
           [1] =&gt; string(1175) "my serialized product object"
        }
     }
     ["flag":protected] =&gt; int(2)
     ["iteratorClass":protected] =&gt; string(13) "ArrayIterator"
     ["protectedProperties":protected] =&gt; NULL
  }
  </code></pre>
  
  <p>Then, if i simply reload the page, or if switch from :</p>
  
  <blockquote>
    <p><a href="http://mydomain.com/products_list/my_product" rel="nofollow">http://mydomain.com/products_list/my_product</a></p>
  </blockquote>
  
  <p>to</p>
  
  <blockquote>
    <p><a href="http://mydomain.com/products_list" rel="nofollow">http://mydomain.com/products_list</a></p>
  </blockquote>
  
  <p>I get :</p>
  
  <pre><code>Debug::dump($_SESSION);
  
  // Display this :
  ["frontCart"] =&gt; NULL
  </code></pre>
  
  <p>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.</p>
  
  <p>Thx</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Following Tim's demand here is more code.</p>
  
  <p>I initialize my session container in the controller's constructor</p>
  
  <pre><code>public function __construct()
  {
      if (!$this-&gt;sessionCart)
      {
          $this-&gt;sessionCart = new Container(ConstantSession::FRONT_CART);
      }
  }
  </code></pre>
  
  <p>Then, here is the exact way i'm adding the product to the container</p>
  
  <pre><code>$this-&gt;sessionCart-&gt;offsetSet(ConstantSession::FRONT_CART_CONTENT,
                                array($cartNumber =&gt; serialize($product))
  );
  </code></pre>
  
  <p>$cartNumber is incremented following the number of products in the cart (when it'll work).
  $product is an object with all its properties.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Following Tim's advises i changed my "add to cart" code to :</p>
  
  <pre><code>$this-&gt;sessionCart-&gt;frontCartContent = array($cartNumber =&gt; $product);
  </code></pre>
  
  <p>When i want to get back my session content i create a new instance of Container :</p>
  
  <pre><code>// Init new container
  $container = new Zend\Session\Container('frontCart');
  
  // Get the content
  $container-&gt;frontCartContent;
  </code></pre>
  
  <p>If i make a Debug::dump() of the last line, i still get NULL after changing page.</p>