Skip to content

Commit

Permalink
Allow specifying namespace when generating routes. (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell authored Dec 21, 2023
1 parent a0ec47f commit 5bc4df3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/customization/route_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/AuthRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit 5bc4df3

Please sign in to comment.