Skip to content

Commit 2ea49af

Browse files
committed
feat: add Module Routing to spark routes
1 parent d02735e commit 2ea49af

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

system/Commands/Utilities/Routes.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\AutoRouteCollector as AutoRouteCollectorImproved;
1919
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
2020
use CodeIgniter\Commands\Utilities\Routes\SampleURIGenerator;
21+
use Config\Routing;
2122
use Config\Services;
2223

2324
/**
@@ -152,6 +153,22 @@ public function run(array $params)
152153
);
153154

154155
$autoRoutes = $autoRouteCollector->get();
156+
157+
// Check for Module Routes.
158+
if ($routingConfig = config(Routing::class)) {
159+
foreach ($routingConfig->moduleRoutes as $uri => $namespace) {
160+
$autoRouteCollector = new AutoRouteCollectorImproved(
161+
$namespace,
162+
$collection->getDefaultController(),
163+
$collection->getDefaultMethod(),
164+
$methods,
165+
$collection->getRegisteredControllers('*'),
166+
$uri
167+
);
168+
169+
$autoRoutes = array_merge($autoRoutes, $autoRouteCollector->get());
170+
}
171+
}
155172
} else {
156173
$autoRouteCollector = new AutoRouteCollector(
157174
$collection->getDefaultNamespace(),

system/Commands/Utilities/Routes/AutoRouterImproved/AutoRouteCollector.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ final class AutoRouteCollector
3535
*/
3636
private array $protectedControllers;
3737

38+
/**
39+
* @var string URI prefix for Module Routing
40+
*/
41+
private string $prefix;
42+
3843
/**
3944
* @param string $namespace namespace to search
4045
*/
@@ -43,13 +48,15 @@ public function __construct(
4348
string $defaultController,
4449
string $defaultMethod,
4550
array $httpMethods,
46-
array $protectedControllers
51+
array $protectedControllers,
52+
string $prefix = ''
4753
) {
4854
$this->namespace = $namespace;
4955
$this->defaultController = $defaultController;
5056
$this->defaultMethod = $defaultMethod;
5157
$this->httpMethods = $httpMethods;
5258
$this->protectedControllers = $protectedControllers;
59+
$this->prefix = $prefix;
5360
}
5461

5562
/**
@@ -82,9 +89,16 @@ public function get(): array
8289
$routes = $this->addFilters($routes);
8390

8491
foreach ($routes as $item) {
92+
$route = $item['route'] . $item['route_params'];
93+
if ($this->prefix !== '' && $route === '/') {
94+
$route = $this->prefix;
95+
} elseif ($this->prefix !== '') {
96+
$route = $this->prefix . '/' . $route;
97+
}
98+
8599
$tbody[] = [
86100
strtoupper($item['method']) . '(auto)',
87-
$item['route'] . $item['route_params'],
101+
$route,
88102
'',
89103
$item['handler'],
90104
$item['before'],

0 commit comments

Comments
 (0)