Skip to content

Commit 7e1d203

Browse files
committed
Add automatic loading of routes files in the Routes folder
1 parent d26da9e commit 7e1d203

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Module.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
*/
2222
abstract class Module extends ServiceProvider implements ModuleContract
2323
{
24+
private const ROUTE_FILE_TYPES = [
25+
'routes', 'web', 'api', 'channels', 'console',
26+
];
27+
2428
/** @var array */
2529
protected $policies = [];
2630

@@ -70,6 +74,7 @@ public function boot(): void
7074
$this->registerListeners();
7175
$this->loadViews();
7276
$this->registerPolicies();
77+
$this->registerRoutes();
7378
$this->registerMiddleware();
7479
}
7580

@@ -246,6 +251,25 @@ private function registerFactories(): void
246251
});
247252
}
248253

254+
private function registerRoutes(): void
255+
{
256+
$routePath = sprintf('%s/Routes', $this->getModulePath());
257+
$routeFilePattern = sprintf('%s/*.php', $routePath);
258+
259+
if (file_exists($routePath) && empty(($files = glob($routeFilePattern))) === false) {
260+
foreach ($files as $file) {
261+
// Skip files that are not allowed route types
262+
if (in_array(rtrim($file, '.php'), static::ROUTE_FILE_TYPES) === false) {
263+
continue;
264+
}
265+
266+
$path = sprintf('%s/%s', $routePath, basename($file));
267+
268+
$this->loadRoutesFrom($path);
269+
}
270+
}
271+
}
272+
249273
/**
250274
* Get the view/config namespace of the current module.
251275
*

0 commit comments

Comments
 (0)