symfony/routing · error · InvalidArgumentException
Route pattern " " cannot contain " " as a path parameter.
Error message
Route pattern "%s" cannot contain "%s" as a path parameter.
What it means
RouteCompiler throws this when a route path contains a variable named "_fragment" or "_firewall". Both are reserved names: "_fragment" selects the fragment renderer and "_firewall" selects the firewall handling the route; allowing them as path parameters would let the request URL choose them. compile() raises an InvalidArgumentException as soon as such a path variable is found.
Solutions
- Rename the path variable to a non-reserved name, e.g. /{section}/dashboard instead of /{_firewall}/dashboard.
- Remove the variable if it was added by mistake and pass the value via defaults or controller arguments instead.
- Search route config files for "_fragment" and "_firewall" used as {placeholders}.
Example fix
// before
$route->setPath('/{_firewall}/dashboard');
// after
$route->setPath('/{area}/dashboard'); Defensive patterns
Strategy: validation
Validate before calling
$reserved = ['_fragment', '_firewall'];
foreach ($reserved as $name) {
if (preg_match('#\{'.preg_quote($name, '#').'\}#', $route->getPath())) {
throw new \InvalidArgumentException("Path must not contain reserved {{$name}} parameter.");
}
} Try / catch
try {
(new RouteCompiler())->compile($route);
} catch (\InvalidArgumentException $e) {
// log and skip/fail fast for the offending route definition
} Prevention
- Treat all underscore-prefixed parameter names as reserved in paths.
- Lint route config (YAML/attributes) in CI for {_fragment} and {_firewall} placeholders.
- Use plain names for regular parameters and pass special values via defaults.
When it happens
Trigger: Compiling a route whose path contains {_fragment} or {_firewall}, e.g. ->setPath('/{_firewall}/dashboard') or a YAML route path '/{_fragment}/foo'.
Common situations: Typos or misunderstandings of Symfony's reserved route attributes; copying _controller-style underscore naming conventions to arbitrary parameter names; dynamically generated route paths that include reserved keys.
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
- Route host " " cannot contain " " as a host parameter.
- Parameter " " for route " " must match " " (" " given) to…
- Parameters for route
- Route aliases cannot be used on non-invokable class
- The " ()" method must not be called.
AI-assisted analysis of symfony/routing@83fa223250 (2026-09-14).
Data as JSON: /api/errors/20440c5f9dfdbdc4.
Report an issue: GitHub.
Appendix: source
Thrown at RouteCompiler.php:86
if (null !== $locale && null !== $route->getDefault('_canonical_route') && preg_quote($locale) === $route->getRequirement('_locale')) {
$requirements = $route->getRequirements();
unset($requirements['_locale']);
$route->setRequirements($requirements);
$route->setPath(str_replace('{_locale}', $locale, $route->getPath()));
}
$path = $route->getPath();
$result = self::compilePattern($route, $path, false);
$staticPrefix = $result['staticPrefix'];
$pathVariables = $result['variables'];
foreach ($pathVariables as $pathParam) {
// "_firewall" selects the firewall handling the route; as a path parameter it would let the URL choose it
if ('_fragment' === $pathParam || '_firewall' === $pathParam) {
throw new \InvalidArgumentException(\sprintf('Route pattern "%s" cannot contain "%s" as a path parameter.', $route->getPath(), $pathParam));
}
}
$variables = array_merge($variables, $pathVariables);
$tokens = $result['tokens'];
$regex = $result['regex'];
return new CompiledRoute(
$staticPrefix,
$regex,
$tokens,
$pathVariables,
$hostRegex,
$hostTokens,
$hostVariables,
array_unique($variables)
);View on GitHub (pinned to 83fa223250)