symfony/routing · error · BadMethodCallException

Cannot serialize Loader\Configurator\ImportConfigurator

Error message

Cannot serialize Loader\Configurator\ImportConfigurator

What it means

ImportConfigurator explicitly forbids serialization via __serialize(), throwing BadMethodCallException. This object holds a live RouteCollection and parent configurator, so serializing it would produce a broken or inconsistent copy; the library treats serialization of it as a programming error.

Solutions

  1. Do not serialize the ImportConfigurator; serialize only the resulting RouteCollection or the raw config array used to build routes.
  2. Rebuild the configurator from configuration (CollectionConfigurator::add()) instead of persisting instances.
  3. If you need dumping, use methods that do not rely on serialization (e.g. inspect the route collection directly).

Example fix

// before
$cache->set('routes_config', $importConfigurator);
// after
$cache->set('routes_collection', $collectionConfigurator->build());
Defensive patterns

Strategy: try-catch

Validate before calling

if ($obj instanceof \Symfony\Component\Routing\Loader\Configurator\ImportConfigurator) { /* never serialize */ }

Type guard

function serializable(mixed $v): bool { return !$v instanceof \Symfony\Component\Routing\Loader\Configurator\ImportConfigurator; }

Try / catch

try { serialize($cfg); } catch (\BadMethodCallException $e) { /* rebuild from config instead */ }

Prevention

When it happens

Trigger: Calling serialize($configurator), var_export/var_dump with serialization side effects, putting an ImportConfigurator in a cache pool or session, or otherwise letting PHP call __serialize() on an ImportConfigurator instance created by CollectionConfigurator::add().

Common situations: Caching route configurator objects, using serialize() to debug route configuration, passing configurators through workflows that clone or persist objects (sessions, queues, var_export-based dumping).

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


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

Appendix: source

Thrown at Loader/Configurator/ImportConfigurator.php:34

/**
 * @author Nicolas Grekas <p@tchwork.com>
 */
class ImportConfigurator
{
    use Traits\HostTrait;
    use Traits\PrefixTrait;
    use Traits\RouteTrait;

    public function __construct(
        private RouteCollection $parent,
        RouteCollection $route,
    ) {
        $this->route = $route;
    }

    public function __serialize(): array
    {
        throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
    }

    public function __unserialize(array $data): void
    {
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
    }

    public function __destruct()
    {
        $this->parent->addCollection($this->route);
    }

    /**
     * Sets the prefix to add to the path of all child routes.
     *
     * @param string|array $prefix the prefix, or the localized prefixes
     *
     * @return $this

View on GitHub (pinned to 83fa223250)