Cookies
Cookies provide a simple way to store small pieces of data on the client’s browser and retrieve them across multiple requests. In SigmaPHP, cookie handling is exposed through a dedicated cookie manager, allowing controllers to read, create, and remove cookies without directly interacting with native PHP functions. This keeps your application consistent and avoids low-level boilerplate code.
The cookie manager can be accessed inside any controller using
$this->cookie(). It allows you to safely manage user preferences,
remember sessions, store temporary tokens, or keep lightweight state between visits.
public function cookieExample()
{
/*
|------------------------------------------------------------
| Creating / Updating a Cookie
|------------------------------------------------------------
*/
$this->cookie()->set(
'theme', // cookie name
'dark', // value
60 * 60 * 24, // lifetime in seconds (1 day)
['path' => '/'] // options
);
// Expected result:
// Browser stores cookie "theme=dark"
/*
|------------------------------------------------------------
| Reading a Cookie
|------------------------------------------------------------
*/
$theme = $this->cookie()->get('theme');
// Expected result:
// "dark"
/*
|------------------------------------------------------------
| Checking if Cookie Exists
|------------------------------------------------------------
*/
if ($this->cookie()->has('theme')) {
// true if cookie exists
}
/*
|------------------------------------------------------------
| Deleting a Cookie
|------------------------------------------------------------
*/
$this->cookie()->delete('theme');
// Expected result:
// Cookie removed from browser
/*
|------------------------------------------------------------
| Typical Usage Example
|------------------------------------------------------------
*/
if (!$this->cookie()->has('visited')) {
$this->cookie()->set('visited', 'yes', 3600);
return $this->response('Welcome, first visit!');
}
return $this->response('Welcome back!');
}
↑
Back to top