CodeIgniter 4 Modules -How I am doing it [Recommendations Required] |
;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); 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 = [ 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 Code: \Modules\Admin\Models Any feedback, suggestions and recommendations will be highly appreciated.
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
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 )
|
Welcome Guest, Not a member yet? Register Sign In |