symfony/routing · error · BadMethodCallException

Cannot unserialize Symfony\Component\Routing\CompiledRoute

Error message

Cannot unserialize Symfony\Component\Routing\CompiledRoute

What it means

CompiledRoute::__unserialize() rejects payloads whose path_prefix, path_regex or host_regex entries are Stringable objects — a format produced by old (pre-4.3-era object-based) serialized CompiledRoute data. This protects against unserializing incompatible legacy cache data; the valid format stores plain strings.

Solutions

  1. Clear and rebuild the cache (rm -rf var/cache/* then cache:warmup) so routes are re-serialized in the current format
  2. Re-generate any persisted serialized CompiledRoute data after upgrading Symfony
  3. Never unserialize CompiledRoute payloads of unknown provenance; store the Route/RouteCollection and compile on demand

Example fix

// before
$route = unserialize($staleCacheData); // old format
// after
rm -rf var/cache/* && php bin/console cache:warmup
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check legacy payloads before unserialize
$data = unserialize($raw, ['allowed_classes' => [CompiledRoute::class]]);
if (isset($data["path_prefix"]) && is_object($data["path_prefix"])) {
    // stale/legacy format — rebuild instead of using
}

Try / catch

try {
    $compiled = unserialize($raw);
} catch (\BadMethodCallException $e) {
    $compiled = $route->compile(); // rebuild from Route
}

Prevention

When it happens

Trigger: unserialize() on a CompiledRoute payload generated by an older Symfony version, or a hand-crafted/modified serialized array containing Stringable values for the regex/prefix keys.

Common situations: Stale routing cache files surviving a major Symfony upgrade; serialized routes persisted in long-lived storage (e.g. DB) across framework upgrades.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of symfony/routing@83fa223250 (2026-09-14). Data as JSON: /api/errors/c363376676e0411f. Report an issue: GitHub.

Appendix: source

Thrown at CompiledRoute.php:63

        return [
            'vars' => $this->variables,
            'path_prefix' => $this->staticPrefix,
            'path_regex' => $this->regex,
            'path_tokens' => $this->tokens,
            'path_vars' => $this->pathVariables,
            'host_regex' => $this->hostRegex,
            'host_tokens' => $this->hostTokens,
            'host_vars' => $this->hostVariables,
        ];
    }

    public function __unserialize(array $data): void
    {
        if (($data['path_prefix'] ?? null) instanceof \Stringable
            || ($data['path_regex'] ?? null) instanceof \Stringable
            || ($data['host_regex'] ?? null) instanceof \Stringable
        ) {
            throw new \BadMethodCallException('Cannot unserialize '.self::class);
        }

        $this->variables = $data['vars'];
        $this->staticPrefix = $data['path_prefix'];
        $this->regex = $data['path_regex'];
        $this->tokens = $data['path_tokens'];
        $this->pathVariables = $data['path_vars'];
        $this->hostRegex = $data['host_regex'];
        $this->hostTokens = $data['host_tokens'];
        $this->hostVariables = $data['host_vars'];
    }

    /**
     * Returns the static prefix.
     */
    public function getStaticPrefix(): string
    {
        return $this->staticPrefix;

View on GitHub (pinned to 83fa223250)