Docs LATEST

SessionManager

API reference for VelvetCMS\Services\SessionManager.

Services

Namespace: VelvetCMS\Services\SessionManager


SessionManager is the lightweight session wrapper used by the HTTP stack and helper functions.

Definition #

class SessionManager
{
    public function __construct();

    public function get(string $key, mixed $default = null): mixed;
    public function set(string $key, mixed $value): void;
    public function has(string $key): bool;
    public function delete(string $key): void;

    public function flash(string $key, mixed $value): void;
    public function getFlash(string $key, mixed $default = null): mixed;
    public function ageFlashData(): void;

    public function all(): array;
    public function clear(): void;
    public function regenerate(bool $deleteOld = true): bool;
    public function token(): string;
    public function regenerateToken(): void;
    public function isStarted(): bool;
}

Behavior notes #

  • Dot notation is supported for nested session keys.
  • Flash data is stored under _flash.new and _flash.old.
  • token() lazily creates the CSRF token and stores it under _token.
  • regenerate() only succeeds when a PHP session is already active.

Examples #

$session = app('session');

$session->set('user.id', 42);
$session->flash('notice', 'Profile updated.');

$userId = $session->get('user.id');
$message = $session->getFlash('notice');

You can also use the global helper:

$session = session();
$locale = session('preferences.locale', 'en');