diff --git a/system/Config/Routes.php b/system/Config/Routes.php index 14735e27432f..15298ce6cf2a 100644 --- a/system/Config/Routes.php +++ b/system/Config/Routes.php @@ -9,8 +9,6 @@ * the LICENSE file that was distributed with this source code. */ -use CodeIgniter\Exceptions\PageNotFoundException; - /* * System URI Routing * @@ -21,11 +19,6 @@ * already loaded up and ready for us to use. */ -// Prevent access to initController method -$routes->add('(:any)/initController', static function () { - throw PageNotFoundException::forPageNotFound(); -}); - // Migrations $routes->cli('migrations/(:segment)/(:segment)', '\CodeIgniter\Commands\MigrationsCommand::$1/$2'); $routes->cli('migrations/(:segment)', '\CodeIgniter\Commands\MigrationsCommand::$1'); diff --git a/system/Router/Router.php b/system/Router/Router.php index fad7d45ee5f6..e655f147632e 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -485,6 +485,11 @@ public function autoRoute(string $uri) $this->method = array_shift($segments) ?: $this->method; } + // Prevent access to initController method + if (strtolower($this->method) === 'initcontroller') { + throw PageNotFoundException::forPageNotFound(); + } + if (! empty($segments)) { $this->params = $segments; } diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index af8588851ea7..3ba45ee03351 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -362,6 +362,16 @@ public function testAutoRouteRejectsMidDot() $router->autoRoute('Foo.bar'); } + public function testAutoRouteRejectsInitController() + { + $router = new Router($this->collection, $this->request); + $router->setTranslateURIDashes(true); + + $this->expectException(PageNotFoundException::class); + + $router->autoRoute('home/initController'); + } + public function testDetectsLocales() { $router = new Router($this->collection, $this->request);