Skip to content

Commit

Permalink
Fix bug could not get :id when rule /:controller/:action/:id thx to @…
Browse files Browse the repository at this point in the history
…yuya_takeyama
  • Loading branch information
heavenshell committed Apr 18, 2011
1 parent f37b7e1 commit 484f6fa
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Net/URL/Dispatcher.php
Expand Up @@ -425,6 +425,14 @@ public function dispatch($directory = null)
$params = $this->parseRequests($params);
$params = self::deleteNullByte($params);

unset($match['controller']);
unset($match['action']);
unset($match['params']);

foreach ($match as $key => $value) {
$params[$key] = $value;
}

if (!is_null($this->_params)) {
if (is_array($this->_params) && is_array($params)) {
$params = array_merge($params, $this->_params);
Expand Down
49 changes: 49 additions & 0 deletions tests/DispatcherTest.php
Expand Up @@ -124,6 +124,55 @@ public function testShouldSetParams()
$this->assertSame($buffer, 'fuga_bar');
}

public function testShouldSetCustomParam()
{
$path = dirname(__FILE__) . '/apps/sample/';
$dispatcher = new Net_URL_Dispatcher(__METHOD__);
$_ENV['PATH_INFO'] = 'hoge/getid/1';
ob_start();
$dispatcher->connect(':controller/:action/:id')->dispatch($path);
$buffer = ob_get_contents();
ob_end_clean();
$this->assertSame($buffer, 'id=1');

$_ENV['PATH_INFO'] = 'hoge/customparams/1/foo';
ob_start();
$dispatcher->connect(':controller/:action/:id/:name')->dispatch($path);
$buffer = ob_get_contents();
ob_end_clean();
$this->assertSame($buffer, 'id=1, name=foo');
}

public function testShouldSetCustomParams()
{
$path = dirname(__FILE__) . '/apps/sample/';
$dispatcher = new Net_URL_Dispatcher(__METHOD__);
$_ENV['PATH_INFO'] = 'hoge/customparams/1/name/foo';
ob_start();
$dispatcher->connect(':controller/:action/:id/*params')->dispatch($path);
$buffer = ob_get_contents();
ob_end_clean();
$this->assertSame($buffer, 'id=1, name=foo');

ob_start();
$dispatcher->connect(':controller/:action/*params')->dispatch($path);
$buffer = ob_get_contents();
ob_end_clean();
$this->assertSame($buffer, 'id=1, name=foo');
}

public function testShouldSetCustomParamAfterWildcard()
{
$path = dirname(__FILE__) . '/apps/sample/';
$dispatcher = new Net_URL_Dispatcher(__METHOD__);
$_ENV['PATH_INFO'] = 'hoge/customparams/name/foo/1';
ob_start();
$dispatcher->connect(':controller/:action/*params/:id')->dispatch($path);
$buffer = ob_get_contents();
ob_end_clean();
$this->assertSame($buffer, 'id=1, name=foo');
}

public function testShouldCallNet_url_mapperClassMethodWhichHasRetrurnValue()
{
$path = dirname(__FILE__) . '/apps/sample/';
Expand Down
13 changes: 13 additions & 0 deletions tests/apps/sample/controllers/HogeController.php
Expand Up @@ -139,4 +139,17 @@ public function getdefaultparamAction()
{
echo $this->getParam('hoge', 'fuga');
}

public function getidAction()
{
$id = $this->getParam('id');
echo "id={$id}";
}

public function customparamsAction()
{
$id = $this->getParam('id');
$name = $this->getParam('name');
echo "id={$id}, name={$name}";
}
}

0 comments on commit 484f6fa

Please sign in to comment.