{"record":{"id":"575b1ff88bb16672","repo":"symfony/routing","slug":"parameter-parameter-for-route-route-must-match-expected","errorCode":null,"errorMessage":"Parameter \"{parameter}\" for route \"{route}\" must match \"{expected}\" (\"{given}\" given) to generate a corresponding URL.","messagePattern":"Parameter \"(.+?)\" for route \"(.+?)\" must match \"(.+?)\" \\(\"(.+?)\" given\\) to generate a corresponding URL\\.","errorType":"validation","errorClass":"InvalidParameterException","httpStatus":null,"severity":"error","filePath":"Generator/UrlGenerator.php","lineNumber":184,"sourceCode":"        // all params must be given\n        if ($diff = array_diff_key($variables, $mergedParams)) {\n            throw new MissingMandatoryParametersException($name, array_keys($diff));\n        }\n\n        $url = '';\n        $optional = true;\n        $message = 'Parameter \"{parameter}\" for route \"{route}\" must match \"{expected}\" (\"{given}\" given) to generate a corresponding URL.';\n        foreach ($tokens as $token) {\n            if ('variable' === $token[0]) {\n                $varName = $token[3];\n                // variable is not important by default\n                $important = $token[5] ?? false;\n\n                if (!$optional || $important || !\\array_key_exists($varName, $defaults) || (null !== $mergedParams[$varName] && (string) $mergedParams[$varName] !== (string) $defaults[$varName])) {\n                    // check requirement (while ignoring look-around patterns)\n                    if (null !== $this->strictRequirements && !preg_match('#^(?:'.preg_replace('/\\(\\?(?:=|<=|!|<!)((?:[^()\\\\\\\\]+|\\\\\\\\.|\\((?1)\\))*)\\)/', '', $token[2]).')$#i'.(empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]] ?? '')) {\n                        if ($this->strictRequirements) {\n                            throw new InvalidParameterException(strtr($message, ['{parameter}' => $varName, '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$varName]]));\n                        }\n\n                        $this->logger?->error($message, ['parameter' => $varName, 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$varName]]);\n\n                        return '';\n                    }\n\n                    $url = $token[1].$mergedParams[$varName].$url;\n                    $optional = false;\n                }\n            } else {\n                // static text\n                $url = $token[1].$url;\n                $optional = false;\n            }\n        }\n\n        if ('' === $url) {","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/Generator/UrlGenerator.php#L166-L202","documentation":"Symfony's UrlGenerator throws InvalidParameterException when a route path variable fails its {requirements} regex while generating a URL. Routing requirements defined in the route are enforced at generation time; look-around assertions are stripped before the check so only the plain requirement is matched. With strictRequirements enabled the exception is thrown instead of being logged and returning an empty string.","triggerScenarios":"Calling $router->generate($route, $params) where a path variable's value does not match the requirement regex (e.g. id='{page}' for a route with '{page}/{page}' requirement, or passing null/empty for a required variable that differs from its default). Only thrown when strictRequirements is true (dev/strict mode).","commonSituations":"Generating URLs for routes with numeric or locale requirements after changing requirements (e.g. adding + at the end of {id}/{d+}); passing strings with slashes or special chars to slug params; generating locale-dependent routes without matching the _locale requirement (e.g. 'en' vs 'en_US').","solutions":["Fix the parameter value at the call site so it satisfies the route requirement shown in the message.","Relax the route's requirement in the route definition if the requirement is too strict.","If non-critical, set strictRequirements=false so a log entry is emitted and generate() returns '' instead of throwing.","Use symfony console debug:router to inspect the actual requirements for the route."],"exampleFix":"// before\n$router->generate('blog_show', ['id' => 'abc']); // route requirement: id => '\\d+'\n// after\n$router->generate('blog_show', ['id' => 123]);\n// or relax:\n// #[Route('/blog/{id}', requirements: ['id' => '[a-zA-Z0-9]+'])]","handlingStrategy":"validation","validationCode":"if (!preg_match('/^\\\\d+$/', (string) $id)) { throw new \\InvalidArgumentException('id must be numeric'); }\n$url = $router->generate('blog_show', ['id' => $id]);","typeGuard":"function isNumericId(mixed $v): bool { return is_int($v) || (is_string($v) && ctype_digit($v)); }","tryCatchPattern":"try { $url = $router->generate($name, $params); } catch (\\Symfony\\Component\\Routing\\Exception\\InvalidParameterException $e) { $this->logger->error('URL generation failed', ['route' => $name, 'prev' => $e]); $url = '/'; }","preventionTips":["Keep strictRequirements=true in dev so violations surface early.","Check route requirements with bin/console debug:router before generating.","Validate user-supplied route parameters against the requirement regex before generate()."],"tags":["symfony","routing","url-generation"],"backgroundTag":"invalid-argument-value","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"}