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.
To integrate laminas/laminas-session into a Laravel project, start by installing the package via Composer:
composer require laminas/laminas-session
Leverage the package for session initialization and data storage:
use Laminas\Session\Container;
use Laminas\Session\SessionManager;
// Start the session
$sessionManager = new SessionManager();
$sessionManager->start();
// Access session data via a container
$cart = new Container('cart');
$cart->offsetSet('items', ['product1', 'product2']);
// Retrieve session data
$items = $cart->offsetGet('items');
SessionManager: Core class for session lifecycle management.Container: Simple key-value storage for session data.Validator: Built-in validators (e.g., CSRF) for session security.Register the session manager in Laravel’s service container for reusable access:
// app/Providers/AppServiceProvider.php
use Laminas\Session\SessionManager;
public function register()
{
$this->app->singleton(SessionManager::class, function () {
$manager = new SessionManager();
$manager->start();
return $manager;
});
}
Laminas\Session\SaveHandler\FileSaveHandler.Laminas\Session\SaveHandler\SaveHandlerInterface for custom storage (e.g., MySQL, Redis).Laminas\Session\SaveHandler\CacheSaveHandler.use Laminas\Session\Validator\Csrf;
// Generate CSRF token
$csrf = new Csrf();
$token = $csrf->generateToken();
// Validate on form submission
$validator = new Csrf();
if (!$validator->isValid($_POST['csrf_token'])) {
throw new \RuntimeException('Invalid CSRF token');
}
Create middleware to initialize sessions and validate tokens:
// app/Http/Middleware/SessionMiddleware.php
public function handle($request, Closure $next)
{
$sessionManager = app(SessionManager::class);
$sessionManager->start();
// Validate CSRF or other session rules
return $next($request);
}
Headers Already Sent:
SessionManager::destroy() after output (e.g., echo, dd()). Use early returns or middleware to prevent this.if (headers_sent()) {
return response()->json(['error' => 'Session cannot be destroyed'], 500);
}
Session Fixation:
$sessionManager->getStorage()->regenerateId(true);
Deprecated Methods:
@deprecated (e.g., Laminas\Db save handlers) will be removed in v3. Migrate to CacheSaveHandler or custom implementations.var_dump($container->get('key')) or Laravel’s dd() to debug session contents.FileSaveHandler (e.g., storage/framework/sessions).
For custom handlers, verify database/Redis connections.Custom Save Handlers:
Implement SaveHandlerInterface for non-file storage (e.g., DynamoDB):
class DynamoSaveHandler implements SaveHandlerInterface {
public function save($sessionId, $data) { /* ... */ }
public function read($sessionId) { /* ... */ }
// ... other required methods
}
Validator Extensions:
Extend Laminas\Session\Validator\AbstractValidator to add custom rules (e.g., session age validation).
Container Inheritance: Use nested containers for modular session data:
$user = new Container('user');
$user->offsetSet('preferences', ['theme' => 'dark']);
SID constant checks are handled (removed in PHP 8.4). Use session_id() directly if needed.SessionManager constructor or config:
$manager = new SessionManager([
'cookie_lifetime' => 3600, // 1 hour
'gc_maxlifetime' => 86400, // 1 day
]);
How can I help you explore Laravel packages today?