Search code examples
phpcodeignitercodeigniter-hmvc

Why HMVC instead of MVC for Codeigniter?


I searched HMVC and know what it is and its advantages.

Modularization: Reduction of dependencies between the disparate parts of the application.

Organization: Having a folder for each of the relevant triads makes for a lighter work load.

Reusability: By nature of the design it is easy to reuse nearly every piece of code.

Extendibility: Makes the application more extensible without sacrificing ease of maintenance.

But just in Codeigniter without HMVC, different sub-folders for controller and multiple model folders and multiple view folders can be created. I do not comprehend the exact reasons for using HMVC.


Solution

  • I found in https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc.

    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.