Skip to content

Commit 5bc4df3

Browse files
authored
Allow specifying namespace when generating routes. (#985)
1 parent a0ec47f commit 5bc4df3

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

docs/customization/route_config.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ $routes->get('register', '\App\Controllers\Auth\RegisterController::registerView
1919

2020
After customization, check your routes with the [spark routes](https://codeigniter.com/user_guide/incoming/routing.html#spark-routes) command.
2121

22+
## Change Namespace
23+
24+
If you are overriding all of the auth controllers, you can specify the namespace as an option to the `routes()` helper:
25+
26+
```php
27+
service('auth')->routes($routes, ['namespace' => '\App\Controllers\Auth']);
28+
```
29+
30+
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`.
31+
2232
## Use Locale Routes
2333

2434
You can use the `{locale}` placeholder in your routes

src/Auth.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ public function routes(RouteCollection &$routes, array $config = []): void
138138
{
139139
$authRoutes = config('AuthRoutes')->routes;
140140

141-
$routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function (RouteCollection $routes) use ($authRoutes, $config): void {
141+
$namespace = $config['namespace'] ?? 'CodeIgniter\Shield\Controllers';
142+
143+
$routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void {
142144
foreach ($authRoutes as $name => $row) {
143145
if (! isset($config['except']) || ! in_array($name, $config['except'], true)) {
144146
foreach ($row as $params) {

tests/Unit/AuthRoutesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,16 @@ public function testRoutesExcept(): void
5151
$this->assertArrayHasKey('logout', $routes);
5252
$this->assertArrayHasKey('auth/a/show', $routes);
5353
}
54+
55+
public function testRoutesCustomNamespace(): void
56+
{
57+
$collection = single_service('routes');
58+
$auth = service('auth');
59+
60+
$auth->routes($collection, ['namespace' => 'Auth']);
61+
62+
$routes = $collection->getRoutes('get');
63+
64+
$this->assertSame('\Auth\RegisterController::registerView', $routes['register']);
65+
}
5466
}

0 commit comments

Comments
 (0)