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.
I've got a few comments; I'm going to address one thing at a time, though.
First, your last example, with the FlashMessenger: we've never had a concept of message types within the FM; all messages are created equal, and any extra semantics you want you have to provide yourself (as you do in your example). TBH, nobody has ever suggested we provide anything more around this -- until recently: Blanchon Vincent recently create a module called "SuperMessenger" (https://github.com/blanchonvincent/SuperMessenger) which has this concept, and simply builds on the FM implementation; I'm hoping to get him to provide us this functionality in the main project in the future.