CodeIgniter Forums
CodeIgniter 4 Modules -How I am doing it [Recommendations Required] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: CodeIgniter 4 Modules -How I am doing it [Recommendations Required] (/showthread.php?tid=77660)



CodeIgniter 4 Modules -How I am doing it [Recommendations Required] - Bennito254 - 10-01-2020

;Hello there.
I have been using CodeIgniter 4 since its release. But of late I have started to dislike the idea of defining module routes in the main Config/Routes.php file.
I came up with a hack, much like the CI3 HMVC/MX library, to auto-load modules Routes.php in a specific directory;

PHP Code:
$di = new RecursiveDirectoryIterator(FCPATH.'Modules'FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($diRecursiveIteratorIterator::SELF_FIRST);

foreach ( 
$ri as $file ) {
    if(!
$file->isDir() && isset(explode('Config/Routes.ph'$file)[1])) {
        include(
$file);
    }



Modules is a directory at the root of the CI4 instance and has its namespace defined in the Autoload.php as bellow

PHP Code:
    public $psr4 = [
        ...
        
'Modules'      => FCPATH 'Modules'
    
]; 

Now all modules directories go in the Modules directory; for example module Admin would have the following structure:


Modules
      Admin
           Config
                   Routes.php  //Here define routes for this module
           Controllers
                 ControllerName.php //Namespace is \Modules\Admin\Controllers
                 ControllerTwo.php
           Models
           ....
     AnotherModule
            Config
                   Routes.php //routes for AnotherModule module
            Controllers
             .......



For this method to work, All classes should be under the \Modules\ namespace; for example
Code:
\Modules\Admin\Controllers
for controllers or
Code:
\Modules\Admin\Models
for models

Any feedback, suggestions and recommendations will be highly appreciated.


RE: CodeIgniter 4 Modules -How I am doing it [Recommendations Required] - nc03061981 - 10-01-2020

In CI4, you can sub folder in Controllers, Models and Views
Example:
Controllers/Admin
Controllers/Frontend
... and ...

Or you can see docs here https://codeigniter.com/user_guide/general/managing_apps.html


RE: CodeIgniter 4 Modules -How I am doing it [Recommendations Required] - InsiteFX - 10-01-2020

If you are creating your own modules in each module you can have all the folders that app has.

(module)
Blog
-- Config
---- Routes.php ( add your roots here! )
-- Controllers
-- etc;

Just keep the namespaces correct.