diff --git a/docs/customization/route_config.md b/docs/customization/route_config.md index 09a15da09..7b06c7d70 100644 --- a/docs/customization/route_config.md +++ b/docs/customization/route_config.md @@ -19,6 +19,16 @@ $routes->get('register', '\App\Controllers\Auth\RegisterController::registerView After customization, check your routes with the [spark routes](https://codeigniter.com/user_guide/incoming/routing.html#spark-routes) command. +## Change Namespace + +If you are overriding all of the auth controllers, you can specify the namespace as an option to the `routes()` helper: + +```php +service('auth')->routes($routes, ['namespace' => '\App\Controllers\Auth']); +``` + +This will generate the routes with the specified namespace instead of the default Shield namespace. This can be combined with any other options, like `except`. + ## Use Locale Routes You can use the `{locale}` placeholder in your routes diff --git a/src/Auth.php b/src/Auth.php index f64e7cbaa..dc4ebb6a9 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -138,7 +138,9 @@ public function routes(RouteCollection &$routes, array $config = []): void { $authRoutes = config('AuthRoutes')->routes; - $routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function (RouteCollection $routes) use ($authRoutes, $config): void { + $namespace = $config['namespace'] ?? 'CodeIgniter\Shield\Controllers'; + + $routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void { foreach ($authRoutes as $name => $row) { if (! isset($config['except']) || ! in_array($name, $config['except'], true)) { foreach ($row as $params) { diff --git a/tests/Unit/AuthRoutesTest.php b/tests/Unit/AuthRoutesTest.php index 9012b731e..c0c5f2741 100644 --- a/tests/Unit/AuthRoutesTest.php +++ b/tests/Unit/AuthRoutesTest.php @@ -51,4 +51,16 @@ public function testRoutesExcept(): void $this->assertArrayHasKey('logout', $routes); $this->assertArrayHasKey('auth/a/show', $routes); } + + public function testRoutesCustomNamespace(): void + { + $collection = single_service('routes'); + $auth = service('auth'); + + $auth->routes($collection, ['namespace' => 'Auth']); + + $routes = $collection->getRoutes('get'); + + $this->assertSame('\Auth\RegisterController::registerView', $routes['register']); + } }