Skip to content
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

Controllers default action not working with more then one method #57

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
},
"require-dev": {
"phpunit/phpunit": "*",
"satooshi/php-coveralls": "dev-master"
"satooshi/php-coveralls": "@stable"
}
}
28 changes: 22 additions & 6 deletions src/Phroute/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
26 changes: 26 additions & 0 deletions test/Dispatcher/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down Expand Up @@ -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));
}



}

}