Simple HMVC - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: External Resources (https://forum.codeigniter.com/forumdisplay.php?fid=7) +--- Forum: Addins (https://forum.codeigniter.com/forumdisplay.php?fid=13) +--- Thread: Simple HMVC (/showthread.php?tid=63078) Pages:
1
2
|
Simple HMVC - Martin7483 - 09-23-2015 Hi everyone! I would like to share my approach of adding HMVC to CodeIgniter. I call it Simple HMVC Simple HMVC is an extension for CodeIgniter 3.x, but should also work in 2.x It makes it possible to use Controller in Controller calls. Direct routing to a HMVC module is not support for one simple reason. I don't believe HMVC should support direct routing. In my opinion I think HMVC is a collection of small independent applications within a lager MVC application. CodeIgniter is that lager MVC application. Simple HMVC makes it possible to use module based applications by only extending the CodeIgniter core Loader. My approach also keeps the loosely coupled system of CodeIgniter intact. The freedom you have in your regular controllers, libraries, models, views and helpers is extended to Simple HMVC. Systems like Modular Extensions – HMVC by WireDesignz make you use a stricter form of coding for your modules. How to install Simple HMVC Step 1 Create a MY_Loader.php file in the directory ./application/core/MY_loader.php If you already have this file edit this file by adding the methods shown in the code below PHP Code: <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); Step 2 Create a directory named modules in your CodeIgniter application directory → ./application/modules Step 3 Create the file Base_module.php in the directory ./application/modules/Base_module.php. Place the code shown below in this file. Keep adding shared code for modules if required to this file PHP Code: <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); Step 4 Create a module directory structure as follows in ./application/modules/:
Create a controller with the same name as your directory your_module in the controllers directory of the module. You can also add sub controllers. Extend controllers from the Base_module class if you wish to use the shared code in your controller Step 6 Build your module application as you would build a small CodeIgniter project! Error display Create a view file in ./application/views/errors/html/error_module.php and add the following to it Code: <?php How to use Simple HMVC From anywhere within CodeIgniter you can load a module like this: PHP Code: $controller = $this->load->module('your_module'); This will load a controller named your_module within the module your_module. This will return the controller object. PHP Code: $controller = $this->load->module('your_module/sub_controller'); This will load a controller named sub_controller within the module your_module. This will return the controller object. PHP Code: $output = $this->load->module('your_module', 'index'); This will load a controller named your_module within the module your_module and call the method index within the controller. The output of the method index will be returned. PHP Code: $params = array(); The same call, but now added params are passed to the index method of the called module controller. The params must be an array. How your controller futher handles it is up to you. Once loaded a module can also be accessed via it's CI super object alias like so: PHP Code: $this->your_controller->some_method(); Module resources can be reused by another module without the need of loading the complete module. You can do this by loading the paths only PHP Code: $this->load->module_paths('some_other_module'); Using config files In the config directory of the module you can add config files for controllers and libraries. Just use the same filename for both controllers and libraries so that config files are auto loaded. Remember to add $config = null to the contructors of the classes All required files have been added as an attachment. I would like to hear what the CI community thinks of my approach EDIT: Have added MIT License RE: Simple HMVC - Martin7483 - 09-24-2015 I have updated the attached zip file RE: Simple HMVC - skunkbad - 09-24-2015 This looks pretty cool. How would you autoload a module or module component? RE: Simple HMVC - Martin7483 - 09-24-2015 I don't have an extended autoload method, and for my own use I won't need one. But what you can do is create a module_loader.php in ./application/libraries and autoload that. Within this module autoloader you can then load any module or module components you want. I will add an example later! RE: Simple HMVC - Martin7483 - 09-25-2015 (09-24-2015, 01:35 PM)skunkbad Wrote: This looks pretty cool. How would you autoload a module or module component? I have added an example Module_loader and have updated the zip file attached to the original post. The Module_loader.php PHP Code: <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); RE: Simple HMVC - kenjis - 09-25-2015 Very interesting. But the license of your code is unknown. Would you make it clear? I hope MIT or BSD license. RE: Simple HMVC - Martin7483 - 09-25-2015 It is under MIT license. Will add it to the original post RE: Simple HMVC - mwhitney - 09-25-2015 I may have missed it somewhere, but don't you need to load the path helper before you can use set_realpath() in your loader? RE: Simple HMVC - Martin7483 - 09-25-2015 (09-25-2015, 06:54 AM)mwhitney Wrote: I may have missed it somewhere, but don't you need to load the path helper before you can use set_realpath() in your loader? Yes, you do and I auto load it. And I have replaced it with my own version of set_realpath() in a MY_path_helper.php file. The file is included in zip file. I should of mentioned it in the beginning. RE: Simple HMVC - kilishan - 09-25-2015 I'm confused. Other than the terminology of the entry class being a "controller" instead of a "library", how is this different than using Packages in CodeIgniter? If you can't route to it then it seems to be basically a package, which exists in CI and allows for any of the standard CI file types except the controller. The only difference that I can see would be that you don't have to grab the CI instance with get_instance() since you're extending from the CI_Controller? |