symfony/routing · error · InvalidArgumentException

Routing requirement for

Error message

Routing requirement for "%s" cannot be empty.

What it means

Route::sanitizeRequirement() strips a leading '^' and trailing '$' (or '\z') anchor from requirement regexes, then throws this InvalidArgumentException if nothing is left — i.e. an empty requirement like '' or '^$' would match only empty strings or nothing useful and is rejected.

Solutions

  1. Provide a non-empty regex, e.g. requirements: { id: '\d+' }
  2. Check any variable interpolated into the requirement is non-empty before building the route
  3. Remove the requirement key entirely if no constraint is needed

Example fix

// before
$route->setRequirement('id', '');
// after
$route->setRequirement('id', '\d+');
Defensive patterns

Strategy: validation

Validate before calling

foreach ($requirements as $k => $r) { if (!is_string($r) || '' === trim($r, '^$\')) { throw new \InvalidArgumentException("Requirement '$k' must be a non-empty regex"); } }

Type guard

function isValidRequirement(mixed $r): bool { return is_string($r) && '' !== trim($r, "^$\z"); }

Try / catch

try { $route->addRequirements($requirements); } catch (\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'cannot be empty')) { /* drop or fix the empty requirement */ } throw $e; }

Prevention

When it happens

Trigger: ->setRequirement('id', '') or ->assert('id', '^$'); a variable interpolated into the requirement that is empty at runtime (e.g. sprintf('{%s}', $pattern) with $pattern = ''); config where requirements: { id: '' }.

Common situations: Building requirement patterns dynamically from env vars/parameters that resolve to empty strings; YAML config with requirements: { id: ~ } coercing oddly; copying patterns and stripping anchors leaving the empty string.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at Route.php:477

    private function sanitizeRequirement(string $key, string $regex): string
    {
        if ('' !== $regex) {
            if ('^' === $regex[0]) {
                $regex = substr($regex, 1);
            } elseif (str_starts_with($regex, '\\A')) {
                $regex = substr($regex, 2);
            }
        }

        if (str_ends_with($regex, '$')) {
            $regex = substr($regex, 0, -1);
        } elseif (\strlen($regex) - 2 === strpos($regex, '\\z')) {
            $regex = substr($regex, 0, -2);
        }

        if ('' === $regex) {
            throw new \InvalidArgumentException(\sprintf('Routing requirement for "%s" cannot be empty.', $key));
        }

        return $regex;
    }

    private function isLocalized(): bool
    {
        return isset($this->defaults['_locale']) && isset($this->defaults['_canonical_route']) && ($this->requirements['_locale'] ?? null) === preg_quote($this->defaults['_locale']);
    }
}

View on GitHub (pinned to 83fa223250)