symfony/symfony · error · RuntimeException

The "app.current_route" variable is not available.

Error message

The "app.current_route" variable is not available.

What it means

AppVariable::getCurrent_route() exposes `app.current_route` and requires a RequestStack injected via setRequestStack(). Without it, the property is uninitialized and the getter throws a RuntimeException before it can read the `_route` attribute from the current request.

Source

Thrown at src/Symfony/Bridge/Twig/AppVariable.php:199

            return $session->getFlashBag()->all();
        }

        if (\is_string($types)) {
            return $session->getFlashBag()->get($types);
        }

        $result = [];
        foreach ($types as $type) {
            $result[$type] = $session->getFlashBag()->get($type);
        }

        return $result;
    }

    public function getCurrent_route(): ?string
    {
        if (!isset($this->requestStack)) {
            throw new \RuntimeException('The "app.current_route" variable is not available.');
        }

        return $this->getRequest()?->attributes->get('_route');
    }

    /**
     * @return array<string, mixed>
     */
    public function getCurrent_route_parameters(): array
    {
        if (!isset($this->requestStack)) {
            throw new \RuntimeException('The "app.current_route_parameters" variable is not available.');
        }

        return $this->getRequest()?->attributes->get('_route_params') ?? [];
    }
}

View on GitHub (pinned to 698e28026c)

Solutions

  1. Inject the request stack via `$appVariable->setRequestStack($requestStack)`.
  2. Ensure a request with the `_route` attribute is pushed onto the stack in non-HTTP contexts.
  3. Pass the current route to templates explicitly when rendering outside a request lifecycle.
  4. Confirm the `twig.app_variable` service wires the request_stack.

Example fix

// before
$app = new \Symfony\Bridge\Twig\AppVariable();
$app->getCurrent_route(); // throws

// after
$app->setRequestStack($requestStack);
$app->getCurrent_route(); // returns _route or null
Defensive patterns

Strategy: validation

Validate before calling

// Push a request with _route before rendering in non-HTTP contexts
$request = \Symfony\Component\HttpFoundation\Request::create('/');
$request->attributes->set('_route', 'current_route_name');
$requestStack->push($request);

Try / catch

try {
    $route = $appVariable->getCurrent_route();
} catch (\RuntimeException $e) {
    $route = null;
}

Prevention

When it happens

Trigger: A template references `{{ app.current_route }}` (or code calls getCurrent_route()) when no RequestStack was wired into AppVariable — same root cause as app.request, since current_route is read from request attributes.

Common situations: Rendering templates that build navigation/breadcrumbs using the current route in CLI, workers, or tests without a request context; standalone Twig missing RequestStack wiring.

Related errors


AI-assisted analysis of symfony/symfony@698e28026c (2026-08-06). Data as JSON: /api/errors/fbdb22769cf36e3b. Report an issue: GitHub.