From ffddc31870be3065e0d9a579cea8454dba525095 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 20 Apr 2026 20:16:25 +0100 Subject: [PATCH 1/4] feat: add `prefix` option for authentication routes --- src/Auth.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Auth.php b/src/Auth.php index fedee8a97..560994505 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -131,14 +131,16 @@ public function authenticate(array $credentials): Result * Usage (in Config/Routes.php): * - auth()->routes($routes); * - auth()->routes($routes, ['except' => ['login', 'register']]) + * - auth()->routes($routes, ['prefix' => 'auth']) */ public function routes(RouteCollection &$routes, array $config = []): void { $authRoutes = config('AuthRoutes')->routes; $namespace = $config['namespace'] ?? 'CodeIgniter\Shield\Controllers'; + $prefix = $config['prefix'] ?? '/'; - $routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void { + $routes->group($prefix, ['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) { From 3feb09cbfdfb171a00bff5344af5725fec32f9ec Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 20 Apr 2026 20:17:12 +0100 Subject: [PATCH 2/4] test: test for `prefix` option in authentication routes --- tests/Unit/AuthRoutesTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Unit/AuthRoutesTest.php b/tests/Unit/AuthRoutesTest.php index d4e568121..271164dd7 100644 --- a/tests/Unit/AuthRoutesTest.php +++ b/tests/Unit/AuthRoutesTest.php @@ -80,4 +80,24 @@ public function testRoutesCustomNamespace(): void $this->assertSame('\Auth\RegisterController::registerView', $routes['register']); } + + public function testRoutesCustomPrefix(): void + { + $collection = single_service('routes'); + $auth = service('auth'); + + $auth->routes($collection, ['prefix' => 'auth']); + + if (version_compare(CodeIgniter::CI_VERSION, '4.5') >= 0) { + $routes = $collection->getRoutes('GET'); + } else { + $routes = $collection->getRoutes('get'); + } + + $this->assertArrayHasKey('auth/register', $routes); + $this->assertArrayHasKey('auth/login', $routes); + $this->assertArrayHasKey('auth/login/magic-link', $routes); + $this->assertArrayHasKey('auth/logout', $routes); + $this->assertArrayHasKey('auth/auth/a/show', $routes); + } } From 4c8cd5bc67211b4012a3b5cf73d2dfe9f270a953 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 20 Apr 2026 20:17:55 +0100 Subject: [PATCH 3/4] docs: add docs for `prefix` option in authentication routes --- docs/customization/route_config.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/customization/route_config.md b/docs/customization/route_config.md index 7b06c7d70..552c8732d 100644 --- a/docs/customization/route_config.md +++ b/docs/customization/route_config.md @@ -29,6 +29,16 @@ 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`. +## Change Prefix + +If you wish, you can prefix all defined authentication routes using the `prefix` option. This is particularly useful if you want all your routes to be under the same root (for example, `auth/login`, `auth/register`). + +```php +service('auth')->routes($routes, ['prefix' => 'auth']); +``` + +This generates routes whose paths are all prefixed with `auth`. + ## Use Locale Routes You can use the `{locale}` placeholder in your routes From cc46a67ba584a244aa60f4f099db095401c5fa21 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Tue, 21 Apr 2026 20:29:26 +0100 Subject: [PATCH 4/4] patch: change `prefix` option to `group` in routes configuration --- docs/customization/route_config.md | 4 ++-- src/Auth.php | 6 +++--- tests/Unit/AuthRoutesTest.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/customization/route_config.md b/docs/customization/route_config.md index 552c8732d..8596e41b6 100644 --- a/docs/customization/route_config.md +++ b/docs/customization/route_config.md @@ -31,10 +31,10 @@ This will generate the routes with the specified namespace instead of the defaul ## Change Prefix -If you wish, you can prefix all defined authentication routes using the `prefix` option. This is particularly useful if you want all your routes to be under the same root (for example, `auth/login`, `auth/register`). +If you wish, you can prefix all defined authentication routes using the `group` option. This is particularly useful if you want all your routes to be under the same root (for example, `auth/login`, `auth/register`). ```php -service('auth')->routes($routes, ['prefix' => 'auth']); +service('auth')->routes($routes, ['group' => 'auth']); ``` This generates routes whose paths are all prefixed with `auth`. diff --git a/src/Auth.php b/src/Auth.php index 560994505..d56d287b8 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -131,16 +131,16 @@ public function authenticate(array $credentials): Result * Usage (in Config/Routes.php): * - auth()->routes($routes); * - auth()->routes($routes, ['except' => ['login', 'register']]) - * - auth()->routes($routes, ['prefix' => 'auth']) + * - auth()->routes($routes, ['group' => 'auth']) */ public function routes(RouteCollection &$routes, array $config = []): void { $authRoutes = config('AuthRoutes')->routes; $namespace = $config['namespace'] ?? 'CodeIgniter\Shield\Controllers'; - $prefix = $config['prefix'] ?? '/'; + $group = $config['group'] ?? '/'; - $routes->group($prefix, ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void { + $routes->group($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 271164dd7..b8b5747f6 100644 --- a/tests/Unit/AuthRoutesTest.php +++ b/tests/Unit/AuthRoutesTest.php @@ -86,7 +86,7 @@ public function testRoutesCustomPrefix(): void $collection = single_service('routes'); $auth = service('auth'); - $auth->routes($collection, ['prefix' => 'auth']); + $auth->routes($collection, ['group' => 'auth']); if (version_compare(CodeIgniter::CI_VERSION, '4.5') >= 0) { $routes = $collection->getRoutes('GET');