Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Session Laravel Package

laminas/laminas-session

Laminas Session provides object-oriented PHP session management: session containers, validators, save handlers, and configuration utilities. Supports secure, testable session workflows for Laminas/Mezzio apps, including storage options and session lifecycle control.

View on GitHub
Deep Wiki
Context7

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](https://github.com/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:

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,
    ];
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky