symfony/routing · error · InvalidParameterException

Parameter " " for route " " must match " " (" " given) to…

Error message

Parameter "{parameter}" for route "{route}" must match "{expected}" ("{given}" given) to generate a corresponding URL.

What it means

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.

Solutions

  1. Fix the parameter value at the call site so it satisfies the route requirement shown in the message.
  2. Relax the route's requirement in the route definition if the requirement is too strict.
  3. If non-critical, set strictRequirements=false so a log entry is emitted and generate() returns '' instead of throwing.
  4. Use symfony console debug:router to inspect the actual requirements for the route.

Example fix

// before
$router->generate('blog_show', ['id' => 'abc']); // route requirement: id => '\d+'
// after
$router->generate('blog_show', ['id' => 123]);
// or relax:
// #[Route('/blog/{id}', requirements: ['id' => '[a-zA-Z0-9]+'])]
Defensive patterns

Strategy: validation

Validate before calling

if (!preg_match('/^\\d+$/', (string) $id)) { throw new \InvalidArgumentException('id must be numeric'); }
$url = $router->generate('blog_show', ['id' => $id]);

Type guard

function isNumericId(mixed $v): bool { return is_int($v) || (is_string($v) && ctype_digit($v)); }

Try / catch

try { $url = $router->generate($name, $params); } catch (\Symfony\Component\Routing\Exception\InvalidParameterException $e) { $this->logger->error('URL generation failed', ['route' => $name, 'prev' => $e]); $url = '/'; }

Prevention

When it happens

Trigger: 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).

Common situations: 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').

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at Generator/UrlGenerator.php:184

        // all params must be given
        if ($diff = array_diff_key($variables, $mergedParams)) {
            throw new MissingMandatoryParametersException($name, array_keys($diff));
        }

        $url = '';
        $optional = true;
        $message = 'Parameter "{parameter}" for route "{route}" must match "{expected}" ("{given}" given) to generate a corresponding URL.';
        foreach ($tokens as $token) {
            if ('variable' === $token[0]) {
                $varName = $token[3];
                // variable is not important by default
                $important = $token[5] ?? false;

                if (!$optional || $important || !\array_key_exists($varName, $defaults) || (null !== $mergedParams[$varName] && (string) $mergedParams[$varName] !== (string) $defaults[$varName])) {
                    // check requirement (while ignoring look-around patterns)
                    if (null !== $this->strictRequirements && !preg_match('#^(?:'.preg_replace('/\(\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\((?1)\))*)\)/', '', $token[2]).')$#i'.(empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]] ?? '')) {
                        if ($this->strictRequirements) {
                            throw new InvalidParameterException(strtr($message, ['{parameter}' => $varName, '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$varName]]));
                        }

                        $this->logger?->error($message, ['parameter' => $varName, 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$varName]]);

                        return '';
                    }

                    $url = $token[1].$mergedParams[$varName].$url;
                    $optional = false;
                }
            } else {
                // static text
                $url = $token[1].$url;
                $optional = false;
            }
        }

        if ('' === $url) {

View on GitHub (pinned to 83fa223250)