Application Integration

Usage in a laminas-mvc Application

The following example shows one potential use case of laminas-session within a laminas-mvc based application. The example uses a module, a controller and the session container.

The example is based on the tutorial application, which builds an album inventory system.

Before starting, make sure laminas-session is installed and configured.

Set up Configuration

To use a session container some configuration for the component is needed:

To allow a reflection-based approach to retrieve the session container from the service manager, a class name is needed as the name for the container. The example uses the name Laminas\Session\Container::class.

Add the following lines to the local or global configuration file, e.g. config/autoload/global.config.php:

return [
    'session_containers' => [
        Laminas\Session\Container::class,
    ],
    'session_storage' => [
        'type' => Laminas\Session\Storage\SessionArrayStorage::class,
    ],
    'session_config'  => [
        'gc_maxlifetime' => 7200,
        // …
    ],
    // …
];

Session Configuration is Optional

The configuration for the session itself is optional, but the factory Laminas\Session\Config\SessionConfig, which is registered for configuration data, expects an array under the key session_config.

A minimum configuration is:

'session_config'  => [],

Create and Register Controller

Create a controller class and inject the session container with the registered classname, e.g. module/Album/Controller/AlbumController.php:

namespace Album\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Session\Container as SessionContainer;

class AlbumController extends AbstractActionController
{
    /** @var SessionContainer */
    private $sessionContainer;

    public function __construct(SessionContainer $sessionContainer)
    {
        $this->sessionContainer = $sessionContainer;
    }
}

To register the controller for the application, extend the configuration of the module. Add the following lines to the module configuration file, e.g. module/Album/config/module.config.php:


namespace Album;

use Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;

return [
    'controllers' => [
        'factories' => [
            // Add this line
            Controller\AlbumController::class => ReflectionBasedAbstractFactory::class,
        ],
    ],
    // …
];

The example uses the reflection factory from laminas-servicemanager to resolve the constructor dependencies for the controller class.

Writing and Reading Session Data

Using the session container in the controller, e.g. module/Album/Controller/AlbumController.php:

Write Data

public function indexAction()
{
    $this->sessionContainer->album = 'I got a new CD with awesome music.';

    return [];
}

Read Data

public function addAction()
{
    return [
        'album_message' => $this->sessionContainer->album ?? null,
    ];
}