From 7c8d3bf44280e026e8ee5c21f5c567fcfc4f457c Mon Sep 17 00:00:00 2001 From: Instrye Date: Tue, 12 May 2020 10:26:52 +0800 Subject: [PATCH] fix. uri have '/' method is '' --- system/Router/Router.php | 6 +++++- tests/system/Router/RouterTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index 3c6e6e01e8bf..ce9d77972683 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -534,7 +534,11 @@ public function autoRoute(string $uri) // has already been set. if (! empty($segments)) { - $this->method = array_shift($segments); + $tempMethod = array_shift($segments); + if ($tempMethod !== '') + { + $this->method = $tempMethod; + } } if (! empty($segments)) diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 419dbe0967e8..44459876c3b7 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -520,4 +520,15 @@ public function testAutoRouteMatchesZeroParams() ]; $this->assertEquals($expected, $router->params()); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/2965 + */ + public function testAutoRouteMethodEmpty() + { + $router = new Router($this->collection, $this->request); + $router->handle('Home/'); + $this->assertEquals('Home', $router->controllerName()); + $this->assertEquals('index', $router->methodName()); + } }