diff --git a/Core/Router.php b/Core/Router.php index 7cc3b09..d2d1f1a 100644 --- a/Core/Router.php +++ b/Core/Router.php @@ -65,7 +65,11 @@ class Router $action = $this->convertToCamelCaps($action); if (is_callable([$object, $action])) { - $object->$action(); + if (array_key_exists('id', $this->params)) { + $var = $this->params['id']; + return $object->$action($var); + } + return $object->$action(); } } } diff --git a/_tests/unit/RouterTest.php b/_tests/unit/RouterTest.php index 1cb561a..6662195 100644 --- a/_tests/unit/RouterTest.php +++ b/_tests/unit/RouterTest.php @@ -20,14 +20,14 @@ class RouterTest extends \PHPUnit\Framework\TestCase [ '{controller}/{action}', [], - 'home/index', - ['controller' => 'home', 'action' => 'index'] + 'posts/new', + ['controller' => 'posts', 'action' => 'new'] ], [ '{controller}/{id:\d+}/{action}', [], - 'home/150/index', - ['controller' => 'home', 'action' => 'index', 'id' => '150'] + 'posts/3/edit', + ['controller' => 'posts', 'action' => 'edit', 'id' => '3'] ] ]; } @@ -51,19 +51,18 @@ class RouterTest extends \PHPUnit\Framework\TestCase /** * * @test - * @dataProvider routeProvider */ - public function dispatchingRouteSuccess($route, $params, $url) + public function dispatchingRouteSuccess() { - // $url = 'home/index'; + $url = 'posts/113/edit'; $router = new Router(); - // $router->add('{controller}/{action}'); - $router->add($route, $params); + $router->add('{controller}/{id:\d+}/{action}'); + // $router->add($route, $params); $this->assertTrue($router->match($url)); - $this->assertFalse($router->dispatch($url)); + $this->assertTrue($router->dispatch($url)); } }