[Routing] ignore trailing slash for non-GET requests
This commit is contained in:
parent
0ca1614aa2
commit
7521af7ea0
@ -118,6 +118,10 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
|||||||
*/
|
*/
|
||||||
protected function matchCollection($pathinfo, RouteCollection $routes)
|
protected function matchCollection($pathinfo, RouteCollection $routes)
|
||||||
{
|
{
|
||||||
|
// HEAD and GET are equivalent as per RFC
|
||||||
|
if ('HEAD' === $method = $this->context->getMethod()) {
|
||||||
|
$method = 'GET';
|
||||||
|
}
|
||||||
$supportsTrailingSlash = '/' !== $pathinfo && '' !== $pathinfo && $this instanceof RedirectableUrlMatcherInterface;
|
$supportsTrailingSlash = '/' !== $pathinfo && '' !== $pathinfo && $this instanceof RedirectableUrlMatcherInterface;
|
||||||
|
|
||||||
foreach ($routes as $name => $route) {
|
foreach ($routes as $name => $route) {
|
||||||
@ -128,7 +132,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
|||||||
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
|
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
|
||||||
if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
|
if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
|
||||||
// no-op
|
// no-op
|
||||||
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods))) {
|
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods)) || 'GET' !== $method) {
|
||||||
continue;
|
continue;
|
||||||
} elseif ('/' === substr($staticPrefix, -1) && substr($staticPrefix, 0, -1) === $pathinfo) {
|
} elseif ('/' === substr($staticPrefix, -1) && substr($staticPrefix, 0, -1) === $pathinfo) {
|
||||||
return $this->allow = array();
|
return $this->allow = array();
|
||||||
@ -149,7 +153,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($hasTrailingSlash && '/' !== substr($pathinfo, -1)) {
|
if ($hasTrailingSlash && '/' !== substr($pathinfo, -1)) {
|
||||||
if (!$requiredMethods || \in_array('GET', $requiredMethods)) {
|
if ((!$requiredMethods || \in_array('GET', $requiredMethods)) && 'GET' === $method) {
|
||||||
return $this->allow = array();
|
return $this->allow = array();
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -168,11 +172,6 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
|||||||
|
|
||||||
// check HTTP method requirement
|
// check HTTP method requirement
|
||||||
if ($requiredMethods) {
|
if ($requiredMethods) {
|
||||||
// HEAD and GET are equivalent as per RFC
|
|
||||||
if ('HEAD' === $method = $this->context->getMethod()) {
|
|
||||||
$method = 'GET';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!\in_array($method, $requiredMethods)) {
|
if (!\in_array($method, $requiredMethods)) {
|
||||||
if (self::REQUIREMENT_MATCH === $status[0]) {
|
if (self::REQUIREMENT_MATCH === $status[0]) {
|
||||||
$this->allow = array_merge($this->allow, $requiredMethods);
|
$this->allow = array_merge($this->allow, $requiredMethods);
|
||||||
|
@ -514,6 +514,17 @@ class UrlMatcherTest extends TestCase
|
|||||||
'customerId' => '123',
|
'customerId' => '123',
|
||||||
);
|
);
|
||||||
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
|
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
|
||||||
|
|
||||||
|
$coll = new RouteCollection();
|
||||||
|
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', array(), array(), array(), '', array(), array('get')));
|
||||||
|
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons', array(), array(), array(), '', array(), array('post')));
|
||||||
|
|
||||||
|
$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
|
||||||
|
$expected = array(
|
||||||
|
'_route' => 'b',
|
||||||
|
'customerId' => '123',
|
||||||
|
);
|
||||||
|
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
|
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
|
||||||
|
Reference in New Issue
Block a user