Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix bug could not get :id when rule /:controller/:action/:id thx to @…

…yuya_takeyama
  • Loading branch information...
commit 484f6fa7921d464da23eb9763ac31725c934bc48 1 parent f37b7e1
heavenshell authored
8 src/Net/URL/Dispatcher.php
View
@@ -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);
49 tests/DispatcherTest.php
View
@@ -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/';
13 tests/apps/sample/controllers/HogeController.php
View
@@ -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}";
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.