Skip to content

[Routing] fix taking verb into account when redirecting #29424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
$staticPrefix = $compiledRoute->getStaticPrefix();
$requiredMethods = $route->getMethods();

// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
// no-op
} elseif (!$supportsTrailingSlash) {
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods))) {
continue;
} elseif ('/' === substr($staticPrefix, -1) && substr($staticPrefix, 0, -1) === $pathinfo) {
return;
return $this->allow = array();
} else {
continue;
}
Expand All @@ -148,7 +149,10 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}

if ($hasTrailingSlash && '/' !== substr($pathinfo, -1)) {
return;
if (!$requiredMethods || \in_array('GET', $requiredMethods)) {
return $this->allow = array();
}
continue;
}

$hostMatches = array();
Expand All @@ -163,7 +167,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}

// check HTTP method requirement
if ($requiredMethods = $route->getMethods()) {
if ($requiredMethods) {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === $method = $this->context->getMethod()) {
$method = 'GET';
Expand All @@ -180,6 +184,8 @@ protected function matchCollection($pathinfo, RouteCollection $routes)

return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : array()));
}

return array();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ public function testFallbackPage()
$this->assertSame(array('_route' => 'foo'), $matcher->match('/foo'));
}

public function testSlashAndVerbPrecedenceWithRedirection()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons', array(), array(), array(), '', array(), array('post')));
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons/', array(), array(), array(), '', array(), array('get')));

$matcher = $this->getUrlMatcher($coll);
$expected = array(
'_route' => 'b',
'customerId' => '123',
);
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons/'));

$matcher->expects($this->once())->method('redirect')->with('/api/customers/123/contactpersons/')->willReturn(array());
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($routes, $context ?: new RequestContext()));
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,20 @@ public function testSchemeAndMethodMismatch()
$matcher->match('/');
}

public function testSlashAndVerbPrecedence()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', array(), array(), array(), '', array(), array('post')));
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons', array(), array(), array(), '', array(), array('get')));

$matcher = $this->getUrlMatcher($coll);
$expected = array(
'_route' => 'b',
'customerId' => '123',
);
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return new UrlMatcher($routes, $context ?: new RequestContext());
Expand Down