phalcon/cphalcon · error · Phalcon\Http\Cookie\Exceptions\CryptServiceUnavailable
A dependency injection container is required to access the '
Error message
A dependency injection container is required to access the 'crypt' service
What it means
Http\Cookie::getValue() with useEncryption(true) must decrypt the raw cookie, which requires the 'crypt' service from the DI container. Cookie is injection-aware; if no container was ever attached (a standalone cookie used without setDI(), or a context where the container was never set), CryptServiceUnavailable is thrown before decryption starts.
Source
Thrown at phalcon/Http/Cookie.zep:208
public function getValue(var filters = null, var defaultValue = null) -> var
{
var container, value, crypt, decryptedValue, filter, signKey, name;
this->checkRestored();
let container = null,
name = this->name;
if this->isRead === false {
if !fetch value, _COOKIE[name] {
return defaultValue;
}
if this->useEncryption {
let container = <DiInterface> this->container;
if container === null {
throw new CryptServiceUnavailable();
}
let crypt = <CryptInterface> container->getShared("crypt");
if unlikely typeof crypt != "object" {
throw new CryptInterfaceRequired();
}
/**
* Verify the cookie's value if the sign key was set
*/
let signKey = this->signKey;
if typeof signKey === "string" {
/**
* Decrypt the value also decoding it with base64
*/
let decryptedValue = crypt->decryptBase64(View on GitHub (pinned to b7419de9cd)
Solutions
- Attach a container before reading: $cookie->setDI($this->getDI()) (or Di::getDefault())
- Ensure the attached container actually has a 'crypt' service registered
- In non-HTTP contexts, decrypt the value manually with the Crypt service instead of relying on the cookie's lazy resolution
Example fix
// before
$cookie = new Cookie('token');
$cookie->useEncryption(true);
$value = $cookie->getValue(); // CryptServiceUnavailable
// after
$cookie = new Cookie('token');
$cookie->useEncryption(true);
$cookie->setDI(Di::getDefault());
$value = $cookie->getValue(); Defensive patterns
Strategy: validation
Validate before calling
if ($cookie->useEncryption() && null === Di::getDefault()) {
throw new \RuntimeException(
'A DI container with a crypt service is required before reading encrypted cookies'
);
}
$value = $cookie->getValue(); Try / catch
try {
$value = $cookie->getValue();
} catch (\Phalcon\Http\Cookie\Exceptions\CryptServiceUnavailable $e) {
// attach the container and retry once
$cookie->setDI(Di::getDefault());
$value = $cookie->getValue();
} Prevention
- Always setDI() on manually constructed Cookie objects before getValue()
- Initialize the DI container (including 'crypt') at the very start of workers and bootstrap scripts
- In tests, inject a container fixture carrying a Crypt service before touching cookies
When it happens
Trigger: new Cookie('token') with useEncryption(true) and getValue() called before any container was attached via setDI(); reading encrypted cookies in CLI workers, queue jobs, or unit tests that skip the full application bootstrap.
Common situations: Background workers reading encrypted cookies passed in job payloads; test harnesses constructing Cookie directly; early bootstrap code that reads cookies before the DI container is configured.
Related errors
- A dependency which implements CryptInterface is required to
- A dependency injection container is required to access the '
- Argument at position {} must have a type
- Service '{}' is required in parameter on position {}
- Service 'value' is required in parameter on position {}
AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21).
Data as JSON: /api/errors/8022d31f9ff9cc07.
Report an issue: GitHub.