{"record":{"id":"3a9467e99ced153a","repo":"symfony/routing","slug":"symfony-component-routing-route-cannot-contain-objects-but-s","errorCode":null,"errorMessage":"Symfony\\Component\\Routing\\Route cannot contain objects, but \"%s\" given.","messagePattern":"Symfony\\\\Component\\\\Routing\\\\Route cannot contain objects, but \"(.+?)\" given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"Matcher/Dumper/CompiledUrlMatcherDumper.php","lineNumber":474,"sourceCode":"\n        return $this->expressionLanguage;\n    }\n\n    private function indent(string $code, int $level = 1): string\n    {\n        return preg_replace('/^./m', str_repeat('    ', $level).'$0', $code);\n    }\n\n    /**\n     * @internal\n     */\n    public static function export(mixed $value): string\n    {\n        if (null === $value) {\n            return 'null';\n        }\n        if (\\is_object($value)) {\n            throw new \\InvalidArgumentException(\\sprintf('Symfony\\Component\\Routing\\Route cannot contain objects, but \"%s\" given.', get_debug_type($value)));\n        }\n        if (!\\is_array($value)) {\n            return str_replace(\"\\n\", '\\'.\"\\n\".\\'', var_export($value, true));\n        }\n        if (!$value) {\n            return '[]';\n        }\n\n        $i = 0;\n        $export = '[';\n\n        foreach ($value as $k => $v) {\n            if ($i === $k) {\n                ++$i;\n            } else {\n                $export .= self::export($k).' => ';\n\n                if (\\is_int($k) && $i < $k) {","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/Matcher/Dumper/CompiledUrlMatcherDumper.php#L456-L492","documentation":"CompiledUrlMatcherDumper::export() serializes route data (defaults, requirements, host tokens, etc.) into PHP code for the compiled matcher class. Route values must be scalars or arrays of scalars; objects are not representable, so any object triggers this InvalidArgumentException.","triggerScenarios":"A route default, requirement, or option containing an object — e.g. 'defaults' => ['locale' => new SomeObject()] — or a Closure stored in defaults (commonly an accidentally-passed controller instance or an object implementing __toString into defaults/requirements).","commonSituations":"Defining routes programmatically and passing non-scalar defaults (e.g. a DateTime, enum without conversion, or Closure); passing an object into ->setRequirement() or options; framework configs where a parameter resolves to an object service instead of a scalar.","solutions":["Use only scalars (string, int, float, bool, null) and arrays of scalars in defaults/requirements/options","If you need an object, store a scalar identifier in the route and resolve the object in the controller/service","Dump debug with get_debug_type() on the offending default before compiling"],"exampleFix":"// before\n$route->setDefault('date', new \\DateTimeImmutable('today'));\n// after\n$route->setDefault('date', (new \\DateTimeImmutable('today'))->format(\\DateTimeInterface::ATOM));","handlingStrategy":"validation","validationCode":"foreach (array_merge($route->getDefaults(), $route->getRequirements()) as $k => $v) { if (is_object($v)) throw new \\LogicException(\"Route value '$k' must be scalar, got \".get_debug_type($v)); }","typeGuard":"function isScalarTree(mixed $v): bool { return is_scalar($v) || is_null($v) || (is_array($v) && array_all($v, fn($x) => isScalarTree($x))); }","tryCatchPattern":"try { $dumper->dump(); } catch (\\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'cannot contain objects')) { /* find the object default */ } throw $e; }","preventionTips":["Keep route defaults/requirements/options strictly scalar","Serialize objects to strings/ids before setDefault()","Add a compile-time assertion that all route values pass a scalar-tree check"],"tags":["routing","serialization","dumper","symfony"],"backgroundTag":"unsupported-operation","analyzedSha":"83fa223250b50f4f018c011e101c330e65ac63cc","analyzedAt":"2026-09-14T03:19:46.280Z","contentChangedAt":"2026-09-14T03:19:46.280Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}