PHP HMVC Tutorial – Hierarchical MVC

PHP HMVC – Overview

Hi, In this tutorial I’m going to demonstrate a simple PHP HMVC implementation.

First, a few words about the differences between MVC and HMVC.




MVC is a design pattern which I’m not going to further explain in here,
Simply because this tutorial is more advanced.

If you don’t know what I’m talking about – You can read more in here.




I will try to put it simple:
PHP HMVC is about having the ability to call other controllers from a child controller.

So, What is HMVC again?

For example, I have a sales site which has categories, items.
My simple site is built using native MVC which I read on many tutorials out there…

My site works pretty well. I have a menu bar with categories, I have many items.
My items in the site are related to my categories in a way that I can choose to display items from a single category…
Pretty much a general BASIC sales site. Right?

Great. Now say my site is expanding and I’m managing a blog right now…
In my blog I want to refer to some item that I sell in my site… How can I do that?

Well, I can do that in 2 ways:

  1. I can write new code in my “blog model” to be able to interact with the items table on my database
  2. I can just call the “item controller” to get my “Item” – That is HMVC

Okay, I understand, now how can I do that?

Hang on, you can’t simply call the “item controller” if you’re on the “blog controller”, can you?
Well. To issue that we need to make a few changes in our MVC code.

  • Until now, we had a basic “bootstrap” class which handled all of the user’s request through “index.php?req=’controller/method/param1/…”
  • As of now, we need to separate the user’s request(external) from our request(internal)

The “Request” Class

abstract class Request
    public function construct()


     * The function receives a request array:
     * @param controller (if not set then it is set to constant DEFAULT_PAGE)
     * @param method (if not set then it is set to default method index() )
     * @param param1
     * @param param2
     * */
    public static function get( $req = array() )
        $defaults = array (
                             "controller" => DEFAULT_PAGE,
                             "method"     => "index",
                             "param1"     => null,
                             "param2"     => null);
        $req = array_merge($defaults, $req);
        $file = 'controller/' . $req['controller'] . '_controller.php';

        $className = $req['controller'] . '_Controller';

        //Check to see if file exists
        if (file_exists($file)) {
            //Require the relevant page controller
            require_once $file;

            $ctrlInstance = new $className($req['controller']);
            //Check to see if the method exists
            if (method_exists($ctrlInstance, $req['method'])) {
                if (isset($req['param1'])) {

                    if (isset($req['param2'])) {
                        //Invokes a controller method with 2 arguments
                        return $ctrlInstance->{$req['method']}($req['param1'], $req['param2']);
                    } else {
                        //Invokes a controller method with an argument
                        return $ctrlInstance->{$req['method']}($req['param1']);
                } else {
                    //Invokes a controller method without an argument
                    return $ctrlInstance->{$req['method']}();

PHP HMVC – The Usage

Finally, how can I use the class?
Say we have a class function in our “item controller” => getItemById( $id ),
However, we’re on the “blog controller” right now…
Solution is below:

$itemReq = Request::get(array("controller" => "item",
                              "method" => "getItemById",
                              "param1" => $id));

It is that simple!

Advantages and Disadvantages


  • You can make sure all your requests (internal and external) go through one place
  • More organized code
  • Less code duplication (You don’t need to write the SQL code to get the item from the DB)
  • It is easier to update models


  • It is a little bit slower in performance matter
  • You have to make sure your code is secure(and not allow external requests to be mixed with internal ones)

Some last words…

Mind that this is a very simplistic PHP HMVC tutorial – as you can extend it much more.
It was made only for understanding how this design pattern works and you should consider it so.

Got any thoughts you would like to share about the whole php HMVC design pattern?
You are more than welcome to comment belowor contact me using our contact form

Tags: , ,

2 Responses to PHP HMVC Tutorial – Hierarchical MVC

  1. Attractive component of content. I simply stumbled upon your weblog and in accession capital to claim that I get actually loved account your weblog posts. Anyway I will be subscribing in your feeds and even I success you get right of entry to constantly rapidly.

  2. Wonderful function! This is the form of info which should be discussed across the website. Shame to the search for machines for not positioning this specific post top! Can happen over plus talk to the site. Thank you so much Equals)

    *** Post forbidden. Need manually approve. Request number 33ed708f99aa5d700457d7f4ab1442c3. Automoderator ***

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>