diff --git a/composer.json b/composer.json index 5c42513..d6d44bf 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,6 @@ }, "require-dev": { "phpunit/phpunit": "*", - "satooshi/php-coveralls": "dev-master" + "satooshi/php-coveralls": "@stable" } } diff --git a/src/Phroute/Dispatcher.php b/src/Phroute/Dispatcher.php index 6e637e5..7cda4d6 100644 --- a/src/Phroute/Dispatcher.php +++ b/src/Phroute/Dispatcher.php @@ -190,15 +190,31 @@ private function dispatchVariableRoute($httpMethod, $uri) } $count = count($matches); - - while(!isset($data['routeMap'][$count++])); - - $routes = $data['routeMap'][$count - 1]; + + while( !isset($data['routeMap'][$count++]) ); + + $routes = $data['routeMap'][$count - 1]; if (!isset($routes[$httpMethod])) { - $httpMethod = $this->checkFallbacks($routes, $httpMethod); - } + try + { + $httpMethod = $this->checkFallbacks($routes, $httpMethod); + + } catch (HttpMethodNotAllowedException $ex) + { // extremely dirty solution + $count = count($matches); + + while( !isset($data['routeMap'][$count++][$httpMethod]) && ( $count <= max(array_keys($data['routeMap'])) ) ); + if(isset($data['routeMap'][$count - 1][$httpMethod])) { + $routes = $data['routeMap'][$count - 1]; + + } else throw new HttpMethodNotAllowedException('Allow: ' . implode(', ', array_keys($routes))); + + } + + } + foreach (array_values($routes[$httpMethod][2]) as $i => $varName) { diff --git a/test/Dispatcher/DispatcherTest.php b/test/Dispatcher/DispatcherTest.php index 720c2c1..a43db8a 100644 --- a/test/Dispatcher/DispatcherTest.php +++ b/test/Dispatcher/DispatcherTest.php @@ -90,6 +90,16 @@ public function getParameterOptionalRequired($param, $param2 = 'default') } } +class DefaultTest{ + function getIndex($one, $two = false){ + return "getIndex"; + } + + function postIndex($one) { + return "postIndex"; + } +} + class DispatcherTest extends \PHPUnit_Framework_TestCase { /** @@ -1068,5 +1078,21 @@ public function provideMethodNotAllowedDispatchCases() return $cases; } + + public function testDefaultControllerActions(){ + $r = $this->router(); + + $r->controller('testDefault', __NAMESPACE__.'\\DefaultTest'); + + try { + $this->dispatch($r, Route::POST, 'testDefault/test'); + $this->dispatch($r, Route::GET, 'testDefault/test'); + } catch (\Phroute\Phroute\Exception\HttpMethodNotAllowedException $ex) { + $this->fail('Should not throw exception '.get_class($ex)); + } + + + + } }