A free and open-source book on ZF3 for beginners


15.4. Session Containers

Once you have configured the session manager, you can actually store and retrieve data to/from session. To do that, you use session containers. Session containers are implemented by the Container class living in Zend\Session namespace.

The session container can be used to save your data to session and retrieve it from session. To avoid naming conflicts between different classes, modules and components that use sessions, the session container allows you to specify the namespace under which the data will be stored. A container namespace may contain upper-case and lower-case characters, underscores and back-slashes. So, for example, "Session\ContainerName", "session_container_name" and "SessionContainerName" are all valid container namespaces.

Session containers work closely with the session manager. When you create a session container, it calls the session manager's start() method automatically, so session is started and initialized.

Now let's start using containers. You can create a container using two equivalent ways: either manually instantiating a container or let a factory do that for you. The second one is easier, so we recommend it.

15.4.1. Method 1. Manual Instantiation of a Session Container

You can just create a session container with the new operator, but you need to pass an instance of the session manager service to container's constructor:

use Zend\Session\Container;

// We assume that $sessionManager variable is an instance of the session manager.
$sessionContainer = new Container('ContainerNamespace', $sessionManager);

So, before you create the container, be sure you have injected the session manager in your controller, service or wherever you need to create the container.

15.4.2. Method 2. Creating a Session Container Using Factory

This method is equivalent to the first one, but the session container is created by the factory. You just need to register what container namespaces you need. To do that, add the session_containers key to your module.config.php file as follows:

<?php
return [
    // ...
    'session_containers' => [
        'ContainerNamespace'
    ],
];

You may list the allowable container names under this key. Choosing a container name is up to you, just be sure it is unique among all other service names.

Once you registered a container name (or several container names), you can create the container and work with it. You typically do that in a factory with the help of the service manager:

// The $container variable is the service manager.
$sessionContainer = $container->get('ContainerNamespace');

As you can see, you retrieve a session container from the service manager by its registered name.

15.4.3. Saving Data to Session with Session Container

When you created the session container, you are able to save data to it as follows:

$sessionContainer->myVar = 'Some data';

To retrieve the data from session container, you use the following code:

if(isset($sessionContainer->myVar))
    $myVar = $sessionContainer->myVar;
else
    $myVar = null;

To remove data from session, use the following code:

unset($sessionContainer->myVar);

For some practical examples of using session containers, please refer to Implementing Multi-Step Forms section.


Top