Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to solve constructor injection of Zend\Session?
    primarykey
    data
    text
    <p>The architecture of the Session component in Zend Framework 2 is yet undocumented and I'm having some trouble understanding it's practical use (compared to the very intuitive Symfony Session for example).</p> <p>A short summary of the important pieces:</p> <ul> <li><code>Zend\Session\Storage\SessionStorage</code> maps and replaces the <code>$_SESSION</code> superglobal</li> <li><code>Zend\Session\SessionManager</code> is a facade to manage the storage, the session cookies, session configuration, session validation, etc.</li> <li><code>Zend\Session\Container</code> is a sort of replacement for the old <code>Session_Namespace</code>, different Containers share one Manager instance (via a static field).</li> </ul> <p>There is no component that represents a collection of namespaces (containers) and therefore there is no way of using methods like 'issetNamespaceX', 'unsetNamespaceX', etc. <em>Nobody</em> (including the Manager as well as Storage) knows about the containers, whether there are any, and if, how many with what names.</p> <p>Matthew Weier O'Phinney explained this circumstance as follows:</p> <blockquote> <p>The Container is a special class for working with isolated segments of the current Storage instance. [...] If anything, a Storage adapter would contain Containers, not the Manager. However, we also want to allow more basic usage of storage, which makes the <strong>Container orthogonal to Storage</strong>, and which explains the difference in has-a relations.</p> </blockquote> <p>I see a couple of practical problems with this solution with regard to proper dependency injection. Obviously the Manager can be seen as a service with a rather long lifetime and therefore qualifies for constructor injection. Unfortunately, the Manager has no clue about the Containers which forces me to either inject Containers as well (bad because rather short lived and takes slots away), write my own additional functionality to make the Storage or the Manager Container-aware (should be framework functionality) or create Containers in my Consuming classes (which I want to avoid obviously).</p> <p>So the Zend solution doesn't seem practical to me. If I want to use the Manager, the FlashMessenger and an additional container, I need to inject 4 (four!) classes. If I do the same with the Symfony Session, I only need to inject 1 (one) class.</p> <p>Furthermore Containers don't qualify for injection since they're potentially short lived run time objects, which may exist or may not at a given point during script execution. With Symfony Session this is not a problem since the Session is aware of it's bags (containers), with ZF2 this is a problem since the Manager is NOT aware of the Containers.</p> <hr> <p><strong>Main question</strong>: How am I supposed to use Zend\Session with Containers in practice?</p> <p>Additional question: Is there a good reason not to provide real namespace functionality similar to <em>ZF1</em> or for example similar to the <em>Symfony</em> <code>SessionBag</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload