[Routing] fix static route reordering when a previous dynamic route conflicts

This commit is contained in:
Nicolas Grekas 2019-08-27 09:38:09 +02:00
parent f1edae020d
commit cba3b6245a
2 changed files with 12 additions and 1 deletions

View File

@ -187,7 +187,7 @@ EOF;
$url = substr($url, 0, -1);
}
foreach ($dynamicRegex as list($hostRx, $rx, $prefix)) {
if (('' === $prefix || 0 === strpos($url, $prefix)) && preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
if (('' === $prefix || 0 === strpos($url, $prefix)) && (preg_match($rx, $url) || preg_match($rx, $url.'/')) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
$dynamicRegex[] = [$hostRegex, $regex, $staticPrefix];
$dynamicRoutes->add($name, $route);
continue 2;

View File

@ -929,6 +929,17 @@ class UrlMatcherTest extends TestCase
$this->assertEquals(['_route' => 'b', 'b' => ''], $matcher->match('/en-en/'));
}
public function testRestrictiveTrailingRequirementWithStaticRouteAfter()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/hello{_}', [], ['_' => '/(?!/)']));
$coll->add('b', new Route('/hello'));
$matcher = $this->getUrlMatcher($coll);
$this->assertEquals(['_route' => 'a', '_' => '/'], $matcher->match('/hello/'));
}
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return new UrlMatcher($routes, $context ?: new RequestContext());