Skip to content

Commit

Permalink
Add automatic loading of routes files in the Routes folder
Browse files Browse the repository at this point in the history
  • Loading branch information
rdgout committed Sep 15, 2020
1 parent d26da9e commit 7e1d203
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
*/
abstract class Module extends ServiceProvider implements ModuleContract
{
private const ROUTE_FILE_TYPES = [
'routes', 'web', 'api', 'channels', 'console',
];

/** @var array */
protected $policies = [];

Expand Down Expand Up @@ -70,6 +74,7 @@ public function boot(): void
$this->registerListeners();
$this->loadViews();
$this->registerPolicies();
$this->registerRoutes();
$this->registerMiddleware();
}

Expand Down Expand Up @@ -246,6 +251,25 @@ private function registerFactories(): void
});
}

private function registerRoutes(): void
{
$routePath = sprintf('%s/Routes', $this->getModulePath());
$routeFilePattern = sprintf('%s/*.php', $routePath);

if (file_exists($routePath) && empty(($files = glob($routeFilePattern))) === false) {
foreach ($files as $file) {
// Skip files that are not allowed route types
if (in_array(rtrim($file, '.php'), static::ROUTE_FILE_TYPES) === false) {
continue;
}

$path = sprintf('%s/%s', $routePath, basename($file));

$this->loadRoutesFrom($path);
}
}
}

/**
* Get the view/config namespace of the current module.
*
Expand Down

0 comments on commit 7e1d203

Please sign in to comment.