Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying namespace when generating routes. #985

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}
}
Loading