Skip to content
Permalink
Browse files

Account for HTTP method when matching routes.

Prior to this change routes using more than one HTTP method could not
ever be matched with reverse routing.
  • Loading branch information...
markstory committed Jul 14, 2014
1 parent 799da32 commit 0273d9fcb4791992d2b75c96711306afa406b1ce
Showing with 63 additions and 0 deletions.
  1. +24 −0 src/Routing/Route/Route.php
  2. +39 −0 tests/TestCase/Routing/Route/RouteTest.php
@@ -438,6 +438,12 @@ public function match(array $url, array $context = []) {
unset($url['_ext']);
}
// Check the method first as it is special.
if (!$this->_matchMethod($url)) {
return false;
}
unset($url['[method]'], $defaults['[method]']);
// Missing defaults is a fail.
if (array_diff_key($defaults, $url) !== []) {
return false;
@@ -500,6 +506,24 @@ public function match(array $url, array $context = []) {
return $this->_writeUrl($url, $pass, $query);
}
/**
* Check whether or not the URL's HTTP method matches.
*
* @param array $url The array for the URL being generated.
*/
protected function _matchMethod($url) {
if (empty($this->defaults['[method]'])) {
return true;
}
if (empty($url['[method]'])) {
return false;
}
if (!in_array(strtoupper($url['[method]']), (array)$this->defaults['[method]'])) {
return false;
}
return true;
}
/**
* Converts a matching route array into a URL string.
*
@@ -680,6 +680,45 @@ public function testParseWithMultipleHttpMethodConditions() {
$this->assertEquals($expected, $route->parse('/sample'));
}
/**
* test that http header conditions can work with URL generation
*
* @return void
*/
public function testMatchWithMultipleHttpMethodConditions() {
$route = new Route('/sample', [
'controller' => 'posts',
'action' => 'index',
'[method]' => ['PUT', 'POST']
]);
$url = [
'controller' => 'posts',
'action' => 'index',
];
$this->assertFalse($route->match($url));
$url = [
'controller' => 'posts',
'action' => 'index',
'[method]' => 'GET',
];
$this->assertFalse($route->match($url));
$url = [
'controller' => 'posts',
'action' => 'index',
'[method]' => 'PUT',
];
$this->assertEquals('/sample', $route->match($url));
$url = [
'controller' => 'posts',
'action' => 'index',
'[method]' => 'POST',
];
$this->assertEquals('/sample', $route->match($url));
}
/**
* Test that the [type] condition works.
*

0 comments on commit 0273d9f

Please sign in to comment.
You can’t perform that action at this time.