D. Caulfield

How to Install Codeigniter HMVC

Update 3rd February 2021: The below post has been updated with new links and clarifications. HMVC Codeigniter should work with PHP7.3 and Codeigniter 3.1.11.

Codeigniter HMVC is a transformation of the popular MVC (model-view-controller) model that is widely used today. It came about as a solution to some problems that developers came across when using the MVC framework.

Problems with MVC

HMVC solves most (if not all) of these problems.

Advantages of HMVC

HMVC Structure

The HMVC (Hierarchical Model View Controller) structure is a combination of multiple MVC triads. Each MVC triad is called a module. A controller of one module can call a controller of another module very easily.

Furthermore, we can call other modules from view files. This makes it very easy to insert widgets quickly.

Imgur

Modules

A module contains the model, view and controller folder for a certain section of the app.

For example, if we take http://www.mywebsite.com/welcome, then we know that /welcome is actually a module! So every file that relates to my /welcome page, apart from external libraries, are placed inside the Welcome module =>

This demonstrates how easily managed apps can be, cutting down on time resolving conflicts from different versions of files, and avoiding general confusion.

Now it's time to install our own HMVC framework.

Imgur

Installing Codeigniter MVC

If you have followed the Codeigniter documentation, you should have the MVC file structure setup in your directory like below. Here we can see our controller, models and views folders – which is what the MVC framework looks like.

Imgur

Install HMVC structure

Imgur

Imgur

Transfer files into HMVC framework

Once the above URL loads, this means that HMVC Codeigniter has been successfully loaded. Congratulations!

Redirect from /index.php/welcome to /welcome

When you first access your newly setup HMVC app, you will need to access each module via /index.php/welcome.

If you try to go to /welcome directly, you will get a 404 not found. This is a bit messy - so let's fix it.

Go to the root folder of your Codeigniter application. Create a new file called .htaccess. Paste in the following code.

RewriteEngine on
RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST\_FILENAME} !-f
RewriteCond %{REQUEST\_FILENAME} !-d
RewriteRule ^(.\*)$ index.php/$1 \[L,QSA\]

Note: There are already .htaccess files in Codeigniter - these are not the files to edit. Create a brand new .htaccess file at the root of your directory structure.

Now go to /welcome. You should be able to see your welcome page!

Next Steps

Experiment with your welcome module and learn how codeigniter works. Mess around with the controller.php, create some new views, add in some CSS and get comfortable with your new framework.

Once you are comfortable, it's time to think of an app idea -good luck!