Sessions
A session carries application data between requests from the same browser. Atom's default session uses PHP's native session storage. The session cookie identifies the session, and your application's values are stored on the server.
Using Sessions
Inject Atom\Session\SessionInterface into the service, controller, or page that needs session data. For example, a preference can remember how the current visitor wants to view a list.
use Atom\Session\SessionInterface;
final readonly class ViewPreference
{
public function __construct(private SessionInterface $session)
{
}
public function saveCompactView(): void
{
$this->session->put("view", "compact");
}
public function current(): string
{
return $this->session->get("view", "comfortable");
}
}
put() stores a value and get() reads it, using the supplied default for a missing key. Use has() to check for a key, remove() to forget it, or pull() to read and remove it in one call. Atom saves the session as part of handling the request.
Flash Data
Use Atom\Session\FlashBag for a notice meant for the next request, such as a confirmation after saving preferences. Inject FlashBag alongside the session service where needed.
After saving the preference, put the notice in the flash bag before redirecting:
$flash->put("notice", "View preference saved.");
On the destination page, read and consume it:
$notice = $flash->pull("notice");
pull() removes the notice immediately, so reloading the destination page leaves it empty. Use get() to read it without consuming it. Flash data expires after the next request by default. If that request redirects again, keep("notice") carries the notice through one more request.
Configuration
Set the session cookie's name and policy in the application environment.
SESSION_NAME=ATOMSESSID
SESSION_LIFETIME=0
SESSION_PATH=/
SESSION_HTTP_ONLY=true
SESSION_SAME_SITE=Lax
SESSION_STRICT_MODE=true
When SESSION_SECURE is omitted, Atom enables secure cookies automatically for HTTPS requests. See Cookies for storing ordinary cookie values on the client.