The CookieBag service provides a clean, secure interface for reading from $_COOKIE, writing new cookies, removing them, and managing encrypted values.
It is typically available in controllers as $this->request->getCookieBag()
public function someAction()
{
$cookieBag = $this->request->getCookieBag();
if ($cookieBag->exist('foo')) {
$foo = $cookieBag->get('foo');
// ....
}
}
To use encrypted cookie methods (setEncrypted() and getEncrypted()), you must configure a secret salt in your application configuration.
The salt is a random, long string used to strengthen encryption.
Configuration
Add the following to /config/app.php:
'components' => [
'cookie' => [
'salt' => 'your-long-random-secret-string'
]
]
Important notes
setEncrypted() or getEncrypted() will throw a RuntimeExceptionisEmpty(): bool
Determine whether the current request contains any cookies.
Example
if ($cookieBag->isEmpty()) {
// No cookies received in this request
}
getAll(): array
Retrieve the full array of cookies from the current request.
Example
$cookies = $cookieBag->getAll();
print_r($cookies);
has(string $key): bool
Verify whether a specific cookie key is present in the current request.
Example
if ($cookieBag->has('theme')) {
// Theme preference is set
}
hasMany(array $keys): bool
Verify that all specified cookie keys are present.
Example
if ($cookieBag->hasMany(['user_id', 'session_token'])) {
// Proceed with authenticated logic
}
set(string $key, string $value, int $ttl = TimeHelper::YEAR, string $path = '/', bool $secure = false, bool $httpOnly = false, bool $raw = false): bool
Set or update a cookie. The value becomes immediately available in the current request.
$this->cookieBag->set(
'theme',
'dark',
86000,
'/',
true, // secure (HTTPS only)
true, // httpOnly
false // not raw
);
Important notes
$key cannot contain dots (throws UnexpectedValueException)$value and $key must be scalar typessetcookie() ran successfully (does not guarantee client acceptance)setEncrypted(string $key, string $value, int $ttl = TimeHelper::YEAR, string $path = '/', bool $secure = false, bool $httpOnly = false, bool $raw = false): bool
Set a cookie with its value encrypted using the crypter.
Example
$cookieBag->setEncrypted('token', $token, 8600);
get(string $key): string
Retrieve the value of a cookie by its key. Throws RuntimeException if the key does not exist.
Example
$theme = $cookieBag->get('theme'); // e.g. 'dark'
Note
Use has() first to avoid exceptions if the key may be missing.
getEncrypted(string $key): string
Retrieve and decrypt the value of an encrypted cookie.
Example
$token = $cookieBag->getEncrypted('token');
getOnce(string $key, Closure $callback): mixed
Retrieve a cookie value, or compute and set it once if missing (useful for visitor IDs, preferences).
Example
$visitorId = $cookieBag->getOnce('visitor_id', function () {
return uniqid('vis_', true);
});
remove(string $key): bool
Delete a cookie by expiring it in the past and removing it from the current request.
Example
$cookieBag->remove('session_token');
Note
Returns true if the cookie existed and was removed.
removeAll(): bool
Expire and remove all cookies from the client.
Example
$cookieBag->removeAll(); // Clear all cookies on logout