CodeIgniter Forums
HMVC best practices to reuse code - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: HMVC best practices to reuse code (/thread-27204.html)



HMVC best practices to reuse code - El Forum - 02-03-2010

[eluser]wilso417[/eluser]
Hey guys,

i'm using modular extensions HMVC extension so i can make view partials. I'm working on a CMS right now but I have some questions about best practices.

A lot of my modules are going to need to reuse a lot of the same code whether it be lower level functionality like database calls or reuse common views. Here are some ideas I have to help accomplish this, I am just wondering what would be best approach, best practice, your guys opinions, etc.

- have a base controller that other controllers inherit from that
- have a base model that other models inherit from
- have models call other models
- have controllers call other models
- have multiple controllers in one module
- have libraries do database calls


HMVC best practices to reuse code - El Forum - 02-03-2010

[eluser]theprodigy[/eluser]
I haven't used ME, but I have used Matchbox, so I'll apologize now if my answer is not completely correct.

In Matchbox, it uses a hierarchical search. It first searches the current module directory for the resource you are asking for (controller, module, view, etc). If it can't find it there, it will then search the base (application level) directories. I'm not sure if there is another step, either between those two or afterwards, but it usually finds what I ask for at one of those levels.

According to ME's FAQ page, at the very bottom:
Quote:The model can be stored in the application folder (system/application/models/mymodelname.php) or in the models subdirectory of a module (system/application/modules/mymodule/models/mymodelname.php)

You may want to experiment to see if libraries and views are the same way. If so, then I would take your commonly used functions, and place them at the application level directories where they will be accessible by all your modules. This will make your modules less encapsulated, but I think any way you do it other then multiple copies will do the same.


HMVC best practices to reuse code - El Forum - 02-04-2010

[eluser]gh0st[/eluser]
From my research and work on a Modular CMS and looking at the way PyroCMS works, you need to extend the core controllers, models via the libraries folder.

Quote:- have a base controller that other controllers inherit from that

You need a MY_Controller (it can be named anything though) that lives in Libraries. It will run before anything, you make it include the stuff you want.

Then in your Controllers you make it extend MY_Controller (or whatever you called it).

Quote:- have a base model that other models inherit from

You need a MY_Model that lives in Libraries. Make it include the stuff you want and then your models extend MY_Model.

Jamie Rumbelow did a MY_Model that has basic CRUD/active record functionality out of the box.

The link is here:
http://github.com/jamierumbelow/codeigniter-base-model/


I am not sure about the rest of the other stuff you are after.


HMVC best practices to reuse code - El Forum - 02-04-2010

[eluser]wilso417[/eluser]
Thanks guys from giving me some insight, it's helping me get on the right track. I'm still a little confused on how to organize a couple things.

Our CMS has content modules (news, events, documents, etc) and just regular modules (contact form, flash timeline)

content modules have a lot of common things like:

on the front-end common views, common error pages
on the back-end common things like adding in meta information, title, etc. to a piece of content and setting user/group permissions on that content

regular modules do not nessarily have anything in common with content modules. they will probably be all public, would not allow a cms admin to set the meta information on these, etc.

I think a base controller would be good to do very general things that both of these types of modules would have in common.

but how can i keep common functionality with my content modules? maybe have another controller that content modules inherit from (ie. news controller -> content controller -> base controller) ?


HMVC best practices to reuse code - El Forum - 02-09-2010

[eluser]gh0st[/eluser]
The way I am currently experimenting with is:

Code:
MY_Controller - has basic functionality I need for the entire site
Admin_Controller - has basic functionality for the admin side, and extends MY_Controller
MY_Model - Jamie Rumbelow's MY_Model code, it extends Model.

I am experimenting the auto-loading my own model.
Core_Model (extends MY_Model) and has core methods that are relevant to my system only. This way I don't hack Jamie's code.

Your common functionality modules would be in your MY_Controller because they exist in all areas (from what you describe).

I would recommend downloading PyroCMS and pulling apart what Phil did, that's what I've done/doing; even though the goal is different it certainly has some pointers which I think could be useful.