symfony/http-kernel · error · LogicException

Cannot evaluate Expression for controllers since no…

Error message

Cannot evaluate Expression for controllers since no ExpressionLanguage service was configured.

What it means

ControllerEvent::evaluate() evaluates a controller-reference expression for a #[Cache]/expression-driven attribute. When the expression value is not a Closure, an ExpressionLanguage instance is required to evaluate it; if none was configured (null), the method refuses with a LogicException instead of fataling on a null call.

Solutions

  1. Run "composer require symfony/expression-language" and clear the cache so the ExpressionLanguage service is registered.
  2. Pass an ExpressionLanguage instance when constructing the listener/ControllerEvent in tests.
  3. Replace the string expression with a Closure, which is evaluated without ExpressionLanguage.

Example fix

// before
$event->evaluate('request.getMethod() == "POST"');
// after
$event->evaluate(fn ($args, $request) => $request->getMethod() === 'POST');
Defensive patterns

Strategy: fallback

Validate before calling

// before calling evaluate
if (!is_string($value) || !class_exists(\Symfony\Component\ExpressionLanguage\ExpressionLanguage::class)) { /* use a closure instead */ }

Type guard

$canEvaluate = fn($v): bool => $v instanceof \Closure || class_exists(\Symfony\Component\ExpressionLanguage\ExpressionLanguage::class);

Try / catch

try { $result = $event->evaluate($expr); } catch (\LogicException $e) { $result = $closureFallback($args, $request); }

Prevention

When it happens

Trigger: Calling ControllerEvent->evaluate() with a string expression (or an attribute whose value is a plain string) while the listener was constructed without an ExpressionLanguage, e.g. when the symfony/expression-language package is not installed or not wired into the listener.

Common situations: Using #[AsController]/expression-based attributes in a project missing symfony/expression-language; running unit tests that build the listener without passing the ExpressionLanguage argument; kernel not registering the 'expression_language' service.

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


AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13). Data as JSON: /api/errors/fdfd69a6e57e5c90. Report an issue: GitHub.

Appendix: source

Thrown at Event/ControllerEvent.php:147

    public function evaluate(mixed $value, ?ExpressionLanguage $expressionLanguage, array $args = []): mixed
    {
        if (!$value instanceof \Closure && !$value instanceof Expression) {
            return $value;
        }

        $controller = $this->getController();
        $controller = match (true) {
            \is_object($controller) && !$controller instanceof \Closure => $controller,
            \is_array($controller) && \is_object($controller[0]) => $controller[0],
            default => null,
        };

        if ($value instanceof \Closure) {
            return $value($args, $this->getRequest(), $controller);
        }

        if (!$expressionLanguage) {
            throw new \LogicException('Cannot evaluate Expression for controllers since no ExpressionLanguage service was configured.');
        }

        return $expressionLanguage->evaluate($value, [
            'request' => $this->getRequest(),
            'args' => $args,
            'this' => $controller,
        ]);
    }
}

View on GitHub (pinned to aa3a39d728)