Skip to content

Commit b345976

Browse files
[Routing] fix static route reordering when a previous dynamic route conflicts
1 parent ff1049f commit b345976

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Matcher/Dumper/CompiledUrlMatcherDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private function groupStaticRoutes(RouteCollection $collection): array
187187
$url = substr($url, 0, -1);
188188
}
189189
foreach ($dynamicRegex as list($hostRx, $rx, $prefix)) {
190-
if (('' === $prefix || 0 === strpos($url, $prefix)) && preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
190+
if (('' === $prefix || 0 === strpos($url, $prefix)) && (preg_match($rx, $url) || preg_match($rx, $url.'/')) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
191191
$dynamicRegex[] = [$hostRegex, $regex, $staticPrefix];
192192
$dynamicRoutes->add($name, $route);
193193
continue 2;

Tests/Matcher/UrlMatcherTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,17 @@ public function testTrailingRequirementWithDefault_B()
929929
$this->assertEquals(['_route' => 'b', 'b' => ''], $matcher->match('/en-en/'));
930930
}
931931

932+
public function testRestrictiveTrailingRequirementWithStaticRouteAfter()
933+
{
934+
$coll = new RouteCollection();
935+
$coll->add('a', new Route('/hello{_}', [], ['_' => '/(?!/)']));
936+
$coll->add('b', new Route('/hello'));
937+
938+
$matcher = $this->getUrlMatcher($coll);
939+
940+
$this->assertEquals(['_route' => 'a', '_' => '/'], $matcher->match('/hello/'));
941+
}
942+
932943
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
933944
{
934945
return new UrlMatcher($routes, $context ?: new RequestContext());

0 commit comments

Comments
 (0)