Skip to content

Instantly share code, notes, and snippets.

@markushausammann
Created January 17, 2013 13:59
Show Gist options
  • Save markushausammann/4556064 to your computer and use it in GitHub Desktop.
Save markushausammann/4556064 to your computer and use it in GitHub Desktop.

In this precursor question, I asked about the practical way of using the new Zend\Session component. That question contains a lot of misconceptions I got when first looking at the component.

This question is about the practical way of really doing things.

  • are my proposed ZF ways correct?
  • are there better ways?

For more depth, let's compare how we would do certain things with Zend\Session as well as the Symfony Session component:

Symfony (given you register the namespaced attribute bag):

class Controller {
    private $session;
    public function __construct(Session $session) {
        $this->session = $session;
    }

    //if I want to get a variable from the 'root' namespace
    $this->session->get('key');

    //if I need want to set a variable in a controller specific namespace
    $this->session->set('namespace/key', $variable);

    //if I want to check, if a namespace is set
    $this->session->has('namespace');

    //if I want to add a message to the flash messenger
    $this->session->getFlashBag()->add('info', 'some info');
}

Zend Framework 2:

class Controller {
    private $container; 
    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    //if I want to get a variable from the 'root' namespace. 
    //I guess, I can get the storage first
    //and then check from there, would that work?
    $this->container->getStorage()->offsetGet('key');
    
    //if I need want to set a variable in a controller specific namespace, 
    //that's already the container we injected, so no need to create a namespace
    $this->container->offsetSet('key', $value);

    //if I want to check, if a namespace is set
    //well, it's set because I'm injecting it, so how to verify if it's used... 
    //probably counting elements would do the trick?
    $this->container->getIterator()->count();
    

    //if I want to add a message to the flash messenger 
    //I can use the flashMessenger plugin which is registered automatically, 
    //but it doesn't take care of message type so I have
    //to do that myself, probably by adding an array instead of a message
    $this->flashMessenger()->addMessage(array('type' => 'info', 'message' => 'some info'));
}

It still feels to me like it would be more suitable to inject the storage as session and additionally create a container factory and inject this and then create containers via factory when needed. Whereas with the Symfony Session it seems to completely make sense to inject the session.

@markushausammann
Copy link
Author

I only see that you answered this here today. Thanks a lot. I guess I'll be editing some of it into your SO answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment