diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index 45b0f2306ca2c..3cdc3e460ffaa 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -5,7 +5,7 @@ CHANGELOG ----- * Added support for inline definition of requirements and defaults for host - * Added support for `\A` and `\z` as regex start and end for route requirement + * Added support for `\A` as regex start and `\Z` or `\z` as end for route requirement 5.1.0 ----- diff --git a/src/Symfony/Component/Routing/Route.php b/src/Symfony/Component/Routing/Route.php index 9c852669da662..81adac250783c 100644 --- a/src/Symfony/Component/Routing/Route.php +++ b/src/Symfony/Component/Routing/Route.php @@ -563,7 +563,7 @@ private function sanitizeRequirement(string $key, string $regex) if ('$' === substr($regex, -1)) { $regex = substr($regex, 0, -1); - } elseif (\strlen($regex) - 2 === strpos($regex, '\\z')) { + } elseif (\in_array(\strlen($regex) - 2, [strpos($regex, '\\z'), strpos($regex, '\\Z')], true)) { $regex = substr($regex, 0, -2); } diff --git a/src/Symfony/Component/Routing/Tests/RouteTest.php b/src/Symfony/Component/Routing/Tests/RouteTest.php index 5ad1306d5d747..50eec2115eb66 100644 --- a/src/Symfony/Component/Routing/Tests/RouteTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteTest.php @@ -128,6 +128,10 @@ public function testRequirementAlternativeStartAndEndRegexSyntax() $route->setRequirement('foo', '\A\d+\z'); $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes \A and \z from the path'); $this->assertTrue($route->hasRequirement('foo')); + + $route->setRequirement('bar', '\A\d+\Z'); + $this->assertEquals('\d+', $route->getRequirement('bar'), '->setRequirement() removes \A and \Z from the path'); + $this->assertTrue($route->hasRequirement('bar')); } /** @@ -147,8 +151,10 @@ public function getInvalidRequirements() ['^$'], ['^'], ['$'], + ['\A\Z'], ['\A\z'], ['\A'], + ['\Z'], ['\z'], ]; }