-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Modular Extensions HMVC
Category:Extensions | Category:Extensions::HMVC | Category:Module
[color=red]For the most up to date documentation, look at the [url=http://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home]BitBucket Wiki[/url],[/color]
Modular Extensions makes CodeIgniter modular. Modules are groups of independent CI components (typically, model, controller, view arranged in one application sub-directory) that can be dropped into CodeIgniter applications.
[h3]HMVC stands for Hierarchical Model View Controller.[/h3]
Module Controllers can be used as normal Controllers or HMVC Controllers and they can be used to help you build view partials. Features:
All controllers can contain an $autoload class variable, which holds an array of items to load prior to running the constructor. This can be used together with module/config/autoload.php, however using the $autolaod variable only works for that specific controller.
Modules::run() output is buffered, so any data returned or output directly from the controller is caught and returned to the caller. In particular, $this->load->view() can be used as you would in a normal controller, without the need for return.
Controllers can be loaded as class variables of other controllers using $this->load->module(’module/controller’); or simply $this->load->module(’module’); if the controller name matches the module name.
Any loaded module controller can then be used like a library, ie: $this->module_controller->method(), but it has access to its own models and libraries independently from the caller.
All module controllers are accessible from the URL via module/controller/method or simply module/method if the module and controller names match.
If you add the _remap() method to your controllers you can prevent unwanted access to them and redirect or flag an error as you like.
[h3]Notes[/h3]
Each module may contain a config/routes.php file where routing and a default controller can be defined for that module using $route[‘module_name’] = ‘controller_name’;
Controllers may be loaded from application/controllers subdirectories.
Controllers may also be loaded from module/controllers subdirectories.
Resources may be cross loaded between modules. ie: $this->load->model(‘module/model’);
Use application/libraries/MX_Controller.php to contain Controller class extensions instead of using MY_Controller. Since version 5.3 you may use the standard MY_Controller extension.
Use application/libraries/MX_Loader.php to contain Loader class extensions instead of using MY_Loader. Since version 5.3 you may use the standard MY_Loader extension.
modules::run() is now dedicated to returning view partials only, and it will return buffered output (a view) from a controller. The syntax for using modules::run is a URI style segmented string and unlimited variables. ie: modules::run(‘module/controller/method’,
To call a module controller from within a controller you can use $this->load->module() or modules::load() and PHP5 method chaining is available for any object loaded by ME. ie: $this->load->library(‘validation’)->run().
The PHP5 spl_autoload feature allows you to freely extend your controllers, models and libraries from application/libraries base classes without the need to specifically include or require them.
The library loader has also been updated to accommodate some CI 1.7 features: ie Library aliases are accepted in the same fashion as model aliases, and loading config files from the module config directory as library parameters (re: Form_validation.php) have beed added.
$config = $this->load->config(‘config_file’), Returns the loaded config array to your variable.
Models and libraries can also be loaded from subdirectories in their respective application directories.
[h3]View Partials[/h3]
Using a Module as a view partial from within a view is as easy as writing:
[code]<?php echo modules::run('module/controller/method',
[h3]Support development of Modular Extensions - HMVC[/h3]
[h3]Download[/h3] Download version 5.3.x:[url]http://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc[/url] (CI 1.7 & 2.0 compatible) Download version 5.2:[url=http://modular-extensions-php5.googlecode.com/files/modular_extensions_5231.zip]modular_extensions_5231.zip[/url] (PHP5 only) Download version 4.3: File:modular_extensions_4305.zip
[h3]Module Directory Structure[/h3] Image:hmvc_structure.gif
[h3]Modular Extensions installation[/h3]
-
Start with a clean CI install.
-
Set $config[‘base_url’] correctly for your installation.
-
Access the URL domain/subdir/index.php/welcome => shows Welcome to CodeIgniter
-
Drop Modular Extensions third_party files into the CI 2.0 application/third_party directory or create a new application/third_party directory for CI 1.7
-
Drop Modular Extensions core files into application/core for CI 2.0 or into the application/libraries directory for CI 2.0
-
Access the URL domain/subdir/index.php/welcome => shows Welcome to CodeIgniter
-
Create module directory structure application/modules/welcome/controllers.
-
Move controller application/controllers/welcome.php to application/modules/welcome/controllers/welcome.php.
-
Access the URL domain/subdir/index.php/welcome => shows Welcome to CodeIgniter
-
Create directory application/modules/welcome/views.
-
Move view application/views/welcome_message.php to application/modules/welcome/views/welcome_message.php
-
Access the URL domain/subdir/index.php/welcome => shows Welcome to CodeIgniter
You should now have a running Modular Extensions installation. Installation Guide Hints:
-Steps 1-3 tell you how to get a standard CI install working - if you have a clean/tested CI install, skip to step 4.
-Steps 4-5 show that normal CI still works after installing ME - it shouldn’t interfere with the normal CI setup.
-Steps 6-8 show ME working alongside CI - controller moved to the ME “welcome” module, the view file remains in the CI application/views directory - ME can find module resources in several places, including the application directory.
-Steps 9-11 show ME working with both controller and view in “welcome” module - there should be no files in the application/controllers or application/views directories. FAQ
Q. What are modules, why should I use them?
A. http://en.wikipedia.org/wiki/Module
Q. What is Modular HMVC, why should I use it?
A. Modular HMVC = Multiple MVC triads
This is most useful when you need to load a view and its data within a view. Think about adding a shopping cart to a page. The shopping cart needs its own controller which may call a model to get cart data. Then the controller needs to load the data into a view. So instead of the main controller handling the page and the shopping cart, the shopping cart MVC can be loaded directly in the page. The main controller doesn’t need to know about it, and is totally isolated from it.
In CI we can’t call more than 1 controller per request. Therefore, to achieve HMVC, we have to simulate controllers. It can be done with libraries, or with this “Modular Extensions HMVC” contribution.
The differences between using a library and a “Modular HMVC” HMVC class is: 1) No need to get and use the CI instance within an HMVC class 2) HMVC classes are stored in a modules directory as opposed to the libraries directory.
Q. Is Modular Extensions HMVC the same as Modular Separation?
A. Yes and No. Like Modular Separation, Modular Extensions makes modules “portable” to other installations. For example, if you make a nice self-contained model-controller-view set of files you can bring that MVC into another project by copying just one folder - everything is in one place instead of spread around model, view and controller folders.
Modular HMVC means modular MVC triads. Modular Separation and Modular Extensions allows related controllers, models, libraries, views, etc. to be grouped together in module directories and used like a mini application. But, Modular Extensions goes one step further and allows those modules to “talk” to each other. You can get controller output without having to go out through the http interface again.