symfony/http-kernel · error · LogicException
Unable to use expressions as the Symfony ExpressionLanguage…
Error message
Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".
What it means
CacheAttributeListener::getExpressionLanguage() lazily creates an ExpressionLanguage to evaluate attribute expressions. If the symfony/expression-language package is absent (class does not exist), it throws a LogicException telling the developer to install the component.
Solutions
- Run "composer require symfony/expression-language".
- Remove expression strings from Cache attributes so no ExpressionLanguage is needed.
- Ensure the package is in the 'require' (not require-dev) section of composer.json for production use.
Example fix
// before (composer.json)
"require-dev": { "symfony/expression-language": "^7.0" }
// after
"require": { "symfony/expression-language": "^7.0" } Defensive patterns
Strategy: validation
Validate before calling
if (!class_exists(\Symfony\Component\ExpressionLanguage\ExpressionLanguage::class)) { throw new \RuntimeException('symfony/expression-language is required for Cache expressions'); } Try / catch
try { $response = $controller(); } catch (\LogicException $e) { if (str_contains($e->getMessage(), 'ExpressionLanguage')) { /* install package or bypass attribute */ } throw $e; } Prevention
- Add symfony/expression-language to composer require when using Cache attributes
- Run composer install --no-dev checks in CI to catch pruned packages
- Keep attribute expressions optional with boolean literals in environments lacking the package
When it happens
Trigger: Dispatching a request to a controller with #[Cache(if: 'expression')] (or variables needing evaluation) when symfony/expression-language is not installed via Composer.
Common situations: Production builds pruning dev dependencies where expression-language is only in require-dev; new project forgetting the package while using cache attributes; framework upgrade introducing expression-based attribute options.
Understand the failure class
Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.
Related errors
- Cannot evaluate Expression for controllers since no…
- Request payload contains invalid "form" data.
- Unsupported format: " ".
- Request payload contains invalid
- Request payload contains invalid
AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13).
Data as JSON: /api/errors/819a121d69f9e316.
Report an issue: GitHub.
Appendix: source
Thrown at EventListener/CacheAttributeListener.php:285
}
/**
* @param-immediately-invoked-callable $closureOrExpression
*/
private function evaluate(string|Expression|\Closure $closureOrExpression, array $variables): mixed
{
if ($closureOrExpression instanceof \Closure) {
return $closureOrExpression($variables['args'], $variables['request'], $variables['this']);
}
return $this->getExpressionLanguage()->evaluate($closureOrExpression, $variables);
}
private function getExpressionLanguage(): ExpressionLanguage
{
return $this->expressionLanguage ??= class_exists(ExpressionLanguage::class)
? new ExpressionLanguage()
: throw new \LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
}
private function toSeconds(int|string $time): int
{
if (!is_numeric($time)) {
$now = time();
$time = strtotime($time, $now) - $now;
}
return $time;
}
}
View on GitHub (pinned to aa3a39d728)