DLight\Application\Session

Mô tả: helper static quản lý session + flash message. Session sẽ tự start() khi bạn gọi các method khác.

Namespace / đăng ký

  • Namespace: DLight\Application
  • File: src/Application/Session.php

Public API

  • Session::start(): void
  • Session::get(string $key, mixed $default = null): mixed
  • Session::set(string $key, mixed $value): void
  • Session::has(string $key): bool
  • Session::forget(string $key): void
  • Session::flash(string $key, mixed $value): void
  • Session::getFlash(string $key, mixed $default = null): mixed
  • Session::error(string $message): void
  • Session::success(string $message): void
  • Session::getError(): ?string
  • Session::getSuccess(): ?string
  • Session::regenerate(): void
  • Session::destroy(): void

Cách dùng + demo

Lưu/đọc session:

use DLight\Application\Session;

Session::set('user_id', 123);
$id = Session::get('user_id');

Flash message (tồn tại 1 request sau đó tự “già hoá”):

use DLight\Application\Session;

// Ở request hiện tại (trước khi redirect)
Session::success('Tạo mới thành công!');

// Ở request tiếp theo
$msg = Session::getSuccess();

Bảo mật session:

use DLight\Application\Session;

// Sau khi login
Session::regenerate();

// Logout
Session::destroy();