CodeIgniter Forums

Full Version: CI, HMVC and Beyond
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello fellow CI-users,

I'am working on an web-application. All very simple: filling tables, doing some calculations and show some results. I ran into Codeigniter and HMVC. I thought it to be a nice tool to use, but....
I recognize the potential of the 'H', but there is something missing. I'am still looking for the manual: "This is CI-HMVC and that is how to do it properly."
An example: I wrote a module "Login" to keep track of users and to allow them to signin and signout. You know the procedure. In its model's construct function I create the users table. Write down once and forget, just use. Ouch, there is allready an users table. How to implement "namespacing" at this level ? That would increase re-usability. Or can I use other techniques ? Now, I still have to adapt my Login module. I want to build once and that's it.
Next one: you do not always want a full screen refresh. You hit a link or send a form, your return will rebuild your screen completely. Unless you use ajax. Then you can refresh your screen partly. In my web-app I have a menu: multi level unfolding. When I hit an item I get a refresh of my screen, but also of my menu. I would rather have the menu unfolded with the selected item colored. I can think of a solution for that, but it is not very elegant.
The possibility of calling module functions in HMVC makes me think of times long gone, the time of "Goto" and its tangled code. In a tutorial I saw, after succesful signin, a reload of the main page being hardcoded and session variables added. I would like my code to be re-usable: 'copy-paste, call setup, use' and 'no copy-paste, edit, use', with kind of generic "return to caller function" functions.
That would enhance re-usability and bring CI-HMVC to a higher level. Does anybody has a paragraph of that manual ?

Jerry Afe
HMVC gives you a method for calling code between modules and organizing your code into modules. Most of your questions really have nothing to do with HMVC, but instead with the general idea of separation of concerns in your code.

If you are creating a table in the database, maybe you should use the migrations library to manage the database tables and check the migration version in the model. If that seems like too much for your project, maybe your code to create the table should check whether the table exists first. With any code you intend to use in multiple projects, there is a balance between how much code you are willing to write (and execute as needed) and how much you are willing to assume about the environment in which your code will run.

AJAX vs. a whole-screen refresh: this is really more of the same. Personally, if I write a controller method which I intend to be available via both AJAX and non-AJAX calls, I do something like:
PHP Code:
if ($this->input->is_ajax_request()) {
¬† ¬†return $this->load->view('xyz'$datatrue);

// code to send the view to output 

Most of the time, though, anything I write which is just intended to build a partial view of some sort will just load a view and return it to the caller, rather than sending it to output.

Honestly, most of this is just sitting down and designing code for reusability. A good place to start might be a more general overview, like Wikipedia's page(s) on SOLID OOP design principles ( ).