{"id":744,"date":"2013-05-19T23:16:30","date_gmt":"2013-05-19T21:16:30","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=744"},"modified":"2014-02-14T21:20:17","modified_gmt":"2014-02-14T19:20:17","slug":"hmvc-with-codeigniter-part-1","status":"publish","type":"post","link":"http:\/\/www.extradrm.com\/?p=744","title":{"rendered":"Setup HMVC with Codeigniter 2.1.4"},"content":{"rendered":"<p>HMVC is an evolution of the MVC pattern used for most web applications today. It came as an answer to the salability problems within applications which used MVC. This extension for CodeIgniter enables the use of the Hierarchical Model View Controller(HMVC) pattern and makes your application modular. This allows easy distribution of independent components (MVC) in a single directory across other CodeIgniter applications. All modules are grouped in their own folder and can have their own controller, model, view, library, config, helper and language files. The image below illustrates how this works :<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/hmvc-schema.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-745\" alt=\"hmvc-schema\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/hmvc-schema.png\" width=\"300\" height=\"278\" \/><\/a><\/p>\n<h2>Key advantages to implementing the HMVC pattern :<\/h2>\n<ul>\n<li><strong>Modularization<\/strong>: Reduction of dependencies between the disparate parts of the application.<\/li>\n<li><strong>Organization<\/strong>: Having a folder for each of the relevant triads makes for a lighter work load.<\/li>\n<li><strong>Reusability<\/strong>: By nature of the design it is easy to reuse nearly every piece of code.<\/li>\n<li><strong>Extendibility<\/strong>: Makes the application more extensible without sacrificing ease of maintenance.<\/li>\n<\/ul>\n<p>in the development cycle, these advantages will allow you to get more out of your application with less problems.<\/p>\n<h2><span style=\"text-decoration: underline;\">How to set up HMVC in CodeIgniter<\/span><\/h2>\n<p><strong>It is very simple to setup HMVC in codeigniter :<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Step 1.<\/span><\/strong> Open codeigniter-modular-extensions-hmvc. Download\u00a0 <strong><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/hmvc-codeigniter-modular-extensions.zip\">hmvc-codeigniter-modular-extensions<\/a><\/strong> .I am installing HMVC on CodeIgniter 2.1.3 downloaded on May 2013. I added also image of htaccess file that you can add in hmvc folder root where you are hosted to avoid display of ugly index.php into codeigniter urls (See pictures below).<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Step 2.<\/span><\/strong> Unzip the hmvc modular extensions package.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Step 3.<\/span><\/strong> Move all of the files from the newly extracted folder\u2019s <strong>.\/core\/<\/strong> directory to CodeIgniter\u2019s\/<strong>application\/core\/<\/strong> directory on your server. (See content below)<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Step 4.<\/span><\/strong> Move the <strong>MX directory<\/strong> and all of its files from the newly extracted folder\u2019s .\/third-party\/ directory<br \/>\nto CodeIgniter\u2019s <strong>\/application\/third-party\/<\/strong> directory on your server.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Step 5.<\/span><\/strong> Create a new folder called <strong>modules<\/strong> under applications, where you will create your HMVC modules for a project.<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/hmvc-structure.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-786\" alt=\"hmvc-structure\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/hmvc-structure.jpg\" width=\"197\" height=\"387\" \/><\/a><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/htaccess.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-787\" alt=\"htaccess\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/htaccess.jpg\" width=\"380\" height=\"361\" srcset=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/htaccess.jpg 511w, http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/htaccess-300x285.jpg 300w\" sizes=\"(max-width: 380px) 100vw, 380px\" \/><\/a><\/p>\n<h2><span style=\"text-decoration: underline;\">Important notes about HMVC with Codeigniter 2.1.3<\/span><\/h2>\n<p>To use Modular Separation only, without HMVC, controllers will extend the CodeIgniter Controller class ie:<\/p>\n<pre>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\nclass login extends CI_Controller \r\n{\r\n        function __construct()\r\n        {\r\n            parent::__construct();\r\n        }\r\n}<\/pre>\n<p>Constructors are not required unless you need to load or process something when the controller is first created.<\/p>\n<p>To use HMVC functionality, such as Modules::run(), controllers must extend the MX_Controller class.<br \/>\nYou must use PHP5 style constructors in your controllers. ie:<\/p>\n<pre>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\nclass tasks extends MX_Controller \r\n{\r\n        function __construct()\r\n        {\r\n            parent::__construct();\r\n        }\r\n}<\/pre>\n<p>Controllers may be loaded from application\/controllers sub-directories.<br \/>\nControllers may also be loaded from module\/controllers sub-directories.<br \/>\nResources may be cross loaded between modules. ie:<\/p>\n<pre>$this-&gt;load-&gt;model('module\/model');<\/pre>\n<h2><span style=\"text-decoration: underline;\"><br \/>\nHow tu use Modules Run<\/span><\/h2>\n<p>To use HMVC functionality, such as Modules::run(), controllers must extend the MX_Controller class.<\/p>\n<p>Modules::run() <strong>is designed for returning view partials<\/strong>, 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.<\/p>\n<pre>\/** module and controller names are different, you must include the method name also, including 'index' **\/\r\nmodules::run('module\/controller\/method', $params, $...);\r\n\r\n\/** module and controller names are the same but the method is not 'index' **\/\r\nmodules::run('module\/method', $params, $...);\r\n\r\n\/** module and controller names are the same and the method is 'index' **\/\r\nmodules::run('module', $params, $...);\r\n\r\n\/** Parameters are optional, You may pass any number of parameters. **\/<\/pre>\n<p>To call a module controller from within a controller you can use $this-&gt;load-&gt;module() or Modules::load() and PHP5 method chaining is available for any object loaded by MX. ie:<\/p>\n<pre>$this-&gt;load-&gt;library('validation')-&gt;run().<\/pre>\n<p>To load languages for modules it is recommended to use the Loader method which will pass the active module name to the Lang instance; ie:<\/p>\n<pre>$this-&gt;load-&gt;language('language_file');<\/pre>\n<p>To load helpers inside modules it is recommended to use the Loader method which will pass the active module name to the Helper instance; ie:<\/p>\n<pre>$this-&gt;load-&gt;helper('&lt;module_name&gt;\/&lt;helper_name&gt;')<\/pre>\n<h2>Form validation with MX<\/h2>\n<p>When using form validation with MX you will need to extend the CI_Form_validation class as shown below,<\/p>\n<pre>&lt;?php\r\n\/** application\/libraries\/MY_Form_validation **\/ \r\nclass MY_Form_validation extends CI_Form_validation \r\n{\r\n    public $CI;\r\n}<\/pre>\n<p>before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly. (This has been discussed on the CI forums also).<\/p>\n<pre>&lt;?php\r\nclass tasks extends MX_Controller \r\n{\r\n    function __construct()\r\n    {\r\n        parent::__construct();\r\n\r\n        $this-&gt;load-&gt;library('form_validation');\r\n        $this-&gt;form_validation-&gt;CI =&amp; $this;\r\n    }\r\n}<\/pre>\n<h2>View Partials<\/h2>\n<p>Using a Module as a view partial from within a view is as easy as writing:<\/p>\n<pre>&lt;?php echo Modules::run('module\/controller\/method', $param, $...); ?&gt;<\/pre>\n<p>Parameters are optional, You may pass any number of parameters.<\/p>\n<pre>\r\necho \"&lt;hr&gt;\";\r\necho Modules::run('nofun\/sayhello', $name);\r\n?&gt;\r\n\t\t&lt;?php&gt; \r\n\t\t\/* $this->load->model('YOUR_MODULE_FOLDER_NAME\/MODEL_NAME');*\/\r\n\t\t$this->load->model('acl\/acl_model');\r\n\t\tif(!$this->acl_model->user_has_perm($this->session->userdata('user_id'), 'add_role')): ?&gt;\r\n\t\t&lt;div class=\"well\"&gt;\r\n\t\t\t&lt;h4Add Role&lt;\/h4&gt;\r\n\t\t\t&lt;p&gt;You do not have permission to view this form&lt;\/p&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;?php endif; ?&gt;\r\n<\/pre>\n<p>Now, we will go to the first tutorial (will be available Soonly) &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HMVC is an evolution of the MVC pattern used for most web applications today. It came as an answer to the salability problems within applications which used MVC. This extension for CodeIgniter enables the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2841,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,23],"tags":[358,162,163,164],"youtube_video":null,"_links":{"self":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/744"}],"collection":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=744"}],"version-history":[{"count":0,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2841"}],"wp:attachment":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}