Skip to content

Commit e39151f

Browse files
committed
Allow specifying the location of Routing files within modules.
1 parent 88d45d2 commit e39151f

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

system/Config/Routing.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class Routing extends BaseConfig
2727
APPPATH . 'Routes.php',
2828
];
2929

30+
/**
31+
* When discovering routes within "modules", or namespaces other
32+
* than "App", this is the path relative to the module's root
33+
* directory.
34+
*
35+
* Default: 'Routes.php'
36+
* Legacy: 'Config/Routes.php'
37+
*/
38+
public string $modulePath = 'Routes.php';
39+
3040
/**
3141
* The default namespace to use for Controllers when no other
3242
* namespace has been specified.

system/Router/RouteCollection.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ class RouteCollection implements RouteCollectionInterface
9494
*/
9595
protected array $routeFiles = [];
9696

97+
/**
98+
* The path to the file that contains
99+
* the route definitions within "modules".
100+
*/
101+
protected string $modulePath;
102+
97103
/**
98104
* Defined placeholders that can be used
99105
* within the
@@ -263,6 +269,7 @@ public function __construct(FileLocator $locator, Modules $moduleConfig, Routing
263269
$this->override404 = $routing->override404;
264270
$this->autoRoute = $routing->autoRoute;
265271
$this->routeFiles = $routing->routeFiles;
272+
$this->modulePath = $routing->modulePath;
266273
$this->prioritize = $routing->prioritize;
267274
}
268275

@@ -320,7 +327,12 @@ protected function discoverRoutes()
320327
$routes = $this;
321328

322329
if ($this->moduleConfig->shouldDiscover('routes')) {
323-
$files = $this->fileLocator->search('Config/Routes.php');
330+
if (empty($this->modulePath)) {
331+
log_message('warning', 'Routes module path is not set in Config/Routing.php.');
332+
return;
333+
}
334+
335+
$files = $this->fileLocator->search($this->modulePath);
324336

325337
foreach ($files as $file) {
326338
// Don't include our main file again...

tests/system/Router/RouteCollectionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ protected function getCollector(array $config = [], array $files = [], $moduleCo
5151

5252
$routerConfig = new \Config\Routing();
5353
$routerConfig->defaultNamespace = '\\';
54+
$routerConfig->modulePath = 'Config/Routes.php';
5455

5556
return (new RouteCollection($loader, $moduleConfig, $routerConfig))->setHTTPVerb('get');
5657
}

tests/system/Router/RouterTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ protected function setUp(): void
3636

3737
$moduleConfig = new Modules();
3838
$moduleConfig->enabled = false;
39-
$this->collection = new RouteCollection(Services::locator(), $moduleConfig);
39+
40+
$routerConfig = new \Config\Routing();
41+
$routerConfig->defaultNamespace = '\\';
42+
$routerConfig->modulePath = 'Config/Routes.php';
43+
44+
$this->collection = new RouteCollection(Services::locator(), $moduleConfig, $routerConfig);
4045

4146
$routes = [
4247
'/' => 'Home::index',

user_guide_src/source/general/modules.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ the **Modules** config file, described above.
154154
When working with modules, it can be a problem if the routes in the application contain wildcards.
155155
In that case, see :ref:`routing-priority`.
156156

157+
By default, route files are named **Routes.php** and are located in the root directory of the module. You can change this by setting the ``$modulePath`` variable in the **Routing** config file to path to the file, relative to the module's root directory. For example, if you wanted to put your routes in a file named **Routes.php** in the module's ``Config`` directory, you would set the ``$modulePath`` variable to ``Config/Routes.php``.
158+
157159
Filters
158160
=======
159161

user_guide_src/source/installation/upgrade_433.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ To clean up the routing system, the following changes were made:
4040
- New ``app/Config/Routing.php`` file that holds the settings that used to be in the Routes file.
4141
- The ``app/Config/Routes.php`` file was simplified so that it only contains the routes without settings and verbiage to clutter the file.
4242
- The ``app/Config/Routes.php`` file was moved to ``app/Routes.php`` to make it easier to find. When upgrading, you can change the ``app/Config/Routing.php` file, ``$routeFiles`` property to point to the old location if you prefer.
43+
- Any module ``Routes.php`` files are expected to be in the namespace's root directory now. To adjust this to match the functionality of existing projects, you can cahnge the ``$modulePath`` property in ``app/Config/Routing.php`` to ``'Config/Routes.php'``.
4344
- The environment-specific routes files are no longer loaded automatically. To load those, you must add them to the ``$routeFiles`` property in ``app/Config/Routing.php``.
4445

4546
Config

0 commit comments

Comments
 (0)