Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 Modules -How I am doing it [Recommendations Required]
#1

;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.
Reply
#2

(This post was last modified: 10-01-2020, 02:12 AM by nc03061981.)

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/gener..._apps.html

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#3

(This post was last modified: 10-01-2020, 06:45 AM by InsiteFX.)

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB