CodeIgniter Forums
Load routes from file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Load routes from file (/showthread.php?tid=75966)



Load routes from file - Michal_PB1 - 04-02-2020

Hi,

Is it possible to load routes from a specific file?

For instance, I have:

Module
    - Config
        - BackedRoutes.php
        - FrontRoutes.php
    (...)

or something like that and I would like to load `FrontendRoutes` only for frontend app and `BackedRoutes` for the admin app. Otherwise, if I have one file `Routes` then after added it to front and backend app I can visit all routes - and it is something what I want to eliminate.

The same problem I will have with `Events` file.

Did anyone have an idea how I can resolve that problem?

Thanks all Smile


RE: Load routes from file - InsiteFX - 04-02-2020

If your routes are in a module they will be searched for and loaded.
loaded.


RE: Load routes from file - Michal_PB1 - 04-02-2020

I know and I have it now. I understand that will be discovered and loaded automatically.

I have one module responsible for the frontend and backend part and all necessary routes currently I have in `Routes.php`. My goal is to separate frontend routes from backend routes for that one module.


RE: Load routes from file - FoLez - 04-02-2020

And if you use this code in the Config/Routes.php file
Add its to 14 line
PHP Code:
$request = new Request(new \Config\App);
if ( 
$request->uri->getSegment(1) == 'admin' ) {
    return \
Config\BackedRoutes;
} else {
    return \
Config\FrontRoutes;

and add to line 3 this use namespace 
PHP Code:
use CodeIgniter\HTTP\Request



RE: Load routes from file - Michal_PB1 - 04-03-2020

Thx. Great, that makes sense
(04-02-2020, 02:26 PM)FoLez Wrote: And if you use this code in the Config/Routes.php file
Add its to 14 line
PHP Code:
$request = new Request(new \Config\App);
if ( 
$request->uri->getSegment(1) == 'admin' ) {
    return \
Config\BackedRoutes;
} else {
    return \
Config\FrontRoutes;

and add to line 3 this use namespace 
PHP Code:
use CodeIgniter\HTTP\Request