Tutorial for HMVC Pattern Codeigniter PHP Framework

codeigniter-hmvc-pattern1
Today our tutorial is all about PHP Framework Codeigniter AND HMVC Pattern.

Please check my Tutorial for Codeigniter v3.1.3.

Tutorial for Codeigniter v3.1.3 and HMVC Pattern

So what is Codeigniter Framework?

“CodeIgniter is a proven, agile & open PHP web application framework with a small footprint. It is powering the next generation of web apps.You can check it into their website. Codeigniter by Ellislab.

Most of the developers, from beginner to advance level they know codeigniter, because it is easy to used, great for small to large projects like creating your own CMS (Content Management System) and many web applications that you might want to create.

Before we start to apply HMVC in our Codeigniter Framework, I would like to discuss first the importance of HMVC Pattern in web development.

HMVC is well known for Hierarchical model–view–controller. So what are the benefits of using HMVC Pattern? Well using HMVC you can divide your application by modules.  Yes, that’s right. By dividing your work into modules you can apply new features without editing the others controllers that is not related to your new features. HMVC is also called widgetization because you can used any modules to another modules. That’s cool right?

So enough for short introduction, let’s start to apply an HMVC Pattern to our Codeigniter Framework.

 

REQUIREMENTS:

  1. First and for most, lets download the latest stable version of codeigniter from ellislab website. Download Codeigniter. Currently the latest stable version is 2.1.4. Extract it into your public folder.
  2. Download the Modular Extensions here: HMVC Pattern Download

[divider][/divider]

STEP BY STEP

Let’s apply it to our Codeigniter Framework.

  1. Extract both files into their separate folders, and then look our Modular extension folder by wiredesignz. You will find four files there, the most important files is the core and third_party folder.  (See the image below).
STEP 1 - Tutorial for HMVC Pattern Codeigniter PHP Framework

STEP 1 – Tutorial for HMVC Pattern Codeigniter PHP Framework

 

2. Copy the Core Folder Files, it includes MY_Loader.php and MY_Router.php. Paste it inside our Codeigniter Folders, from application/core/

 

STEP 2 – Tutorial for HMVC Pattern Codeigniter PHP Framework

STEP 2 – Tutorial for HMVC Pattern Codeigniter PHP Framework

3. Go back to our modular extension folder, then copy the files inside third_party folder and paste it to application/third_party folder inside of our codeigniter framework.

 

Tutorial for HMVC Pattern Codeigniter PHP Framework

Tutorial for HMVC Pattern Codeigniter PHP Framework

4. We’re almost done, All of the main modular extensions that we need for our HMVC codeigniter is setup already, what we need now is to test if it is working already. From this point, go to your codeigniter folder then go to application/ folder and create “modules” folder. (See image below).

STEP 4 -Tutorial for HMVC Pattern Codeigniter PHP Framework

STEP 4 -Tutorial for HMVC Pattern Codeigniter PHP Framework

 

5. Using the folder modules, you can create different modules inside.. the structure of the modules is like this.

 

Step 5  - Tutorial for HMVC Pattern Codeigniter PHP Framework

Step 5 – Tutorial for HMVC Pattern Codeigniter PHP Framework

6. Let’s do some test if our modules is working properly.  Go to application/modules/ folder and create “users” folder then create 2 folders and name it, Controllers and Views.

 

STEP 6 - Tutorial for HMVC Pattern Codeigniter PHP Framework

STEP 6 – Tutorial for HMVC Pattern Codeigniter PHP Framework

7. Go to application/modules/users/controllers folder, then create users.php file and insert this code.

8. We will create now our views for our users controller, go to application/modules/users/views/ folder and create a php file named it users_view.php. And insert sample html file. For this tutorial I just used the default codeigniter welcome message. You can copy it for your test.

Welcome to HMVC CodeIgniter Modular Tutorial by Developers.ph

The page you are looking at is being generated dynamically by CodeIgniter.

If you would like to edit this page you’ll find it located at:

application/modules/users/views/users_view.phpThe corresponding controller for this page is found at:

application/modules/users/controllers/users.php

9. Now open up your browser and point your working environment to your codeigniter modular index.php. ONE MORE THING, if you didn’t put an .htaccess in your doc root / root folder you need to put index.php before you call the modules. Example url would like this.

STEP 9 -Tutorial for HMVC Pattern Codeigniter PHP Framework

STEP 9 -Tutorial for HMVC Pattern Codeigniter PHP Framework

  • Red text color is your hostname
  • Blue Text is your index.php IF you didn’t put .htaccess you need to add index.php on it.
  • Yellow text is your module name.

IF you don’t know the .htaccess you can look at here.[highlight] PHP Framework – Codeigniter Htaccess Code[/highlight]

10. Point your browser now to your, development hostname or in wamp/xampp, If you follow everything, you would see this page.

Tutorial for HMVC Pattern Codeigniter PHP Framework Tutorial for HMVC Pattern Codeigniter PHP Framework

CONCLUSION

So now we already know how to setup HMVC Pattern in our Codeigniter Framework. You can now start creating your own cms or system using that pattern.

For the next tutorial we will do a login system using hmvc pattern. Stay tuned guys.

If you have any problems, just comment out below and ill do my best to help you guys.

72384 Total Views 85 Views Today
Anthony Pillos

About Anthony Pillos

Developers.ph – We are Awesome. Tutorials in different programming languages like PHP,HTML5,JAVASCRIPT,CSS/CSS3,JQuery,Jquery Mobile, Responsive Design.

72385 Total Views 86 Views Today

41 comments

      1. I used Codeigniter 3.1.3 and followed your instructions. the same thing sid had, it did not work. I am not sure what he did to make it work. But for me, it seems, CI can’t find the files in core folder.
        Here is the exact error:
        Fatal error: Call to undefined method MY_Loader::_ci_object_to_array() in D:\xampp\htdocs\codeigniter\application\third_party\MX\Loader.php on line 300

  1. Do you have any solutions to “default_controller” in CodeIgniter 3.0 with HMVC? Page – “www.site.com/welcome” works, but when you set the “welcome” as default_controller, there is a 404 error for “www.site.com”…

    1. Hello, if you can give me sample codes you have then i can look at it.. At the moment, i didnt yet tried the HMVC in CI3.0 coz i migrated to laravel 4.2 now… But still ill check CI3.0 then create tutorial for that…

  2. your tutorial is easy understanding, great..

    but, when i downloaded it then Extract and tried this project, i got page not available respone. are there any configuration on specific files like config.php or the other? I’m new with HMVC on CI. But Thank for your tutorial :)

    1. Hello, Is your localhost is properly setup?.. most likely it will work as is.. because i added already .htaccess inside the download file. I was thinking about your php configurations.. is mod_rewrite is on?.

      1. great.. it’s work :D

        i want to ask, I take this project as example. if i change “$this->load->view(‘users_view’);” to “redirect(base_url().’users_view’);” on users controllers, it was bring to page “localhost” page, not welcome page. is “base_url()” not used on HMVC? I use it on MVC and all is fine.

        thank’s..

  3. Hi ,
    thanks for the great tutorial, I need to know how can I load module.
    I need to make one header , one footer and then in each page load different module, so can I do something like this?

    thanks,

  4. I followed the exact same steps but when calling the module it gives me an error.
    Access level to MX_Router::_set_default_controller() must be public (as in class CI_Router) in C:\xampp\htdocs\ci\application\third_party\MX\Router.php on line 241

  5. hello sir..
    as following ur tutorial i have setup hmvc in codeigniter 2.1 but it gives an error when i open with the given url “localhost/index.php/home” (home is module’s name) that the “object not found 404 error”. i m new at hmvc…

    Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.

    Error 404

    localhost
    Apache/2.4.10 (Unix) OpenSSL/1.0.1j PHP/5.6.3 mod_perl/2.0.8-dev Perl/v5.16.3

  6. Are you kidding me
    Im using CI 2.2, followed the tutorial step by step and this is what I got
    Call to undefined method MY_Loader::_ci_load_library() in \application\third_party\MX\Loader.php

  7. As add submodules?
    Example
    myssite/module/controller/action
    myssite/outhermodule/controller/action
    myssite/moremodules/controller/action
    help?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">