Recommended OO approach when using CI |
Sorry if I am driving people a little crazy with all the questions I am in the process of learning CI and want to try and do things the "correct" way from the start.
I come from a procedural background and I am trying hard to adopt a more OO approach. This question might be more PHP related than it is CI related, I'm not sure. Anyway, I am wondering what is the best approach when working with controllers and models in CI. I see an awful lot of examples on the web like this: PHP Code: $this->load->model('customer_model'); The above is an approach I am very familiar with, and seems similar to many of the examples I see in tutorials and so on. However I can't help feeling a better approach is: PHP Code: $this->load->model('customer_model'); Please let me know what you think. I appreciate your thoughts.
05-12-2016, 03:42 PM
(This post was last modified: 05-12-2016, 03:48 PM by PaulD. Edit Reason: Added PS )
I am sorry but I am not really certain what you are asking, however what I like to do now, is to have an additional logic layer. I have my controllers only call libraries (logic layer). Those libraries call models. The model interacts with the database.
The benefit I have found is that say I need to change the way I record customer details. I change the library (making sure any existing functions return the expected values) and the model to deal with the new database structure. All in one place. Any calls to the library still work as expected and I do not have to trawl through every controller where a customer detail is called. The added library level allows me to put logic into the process, whilst keeping the model as thin as possible. So I have a thin(ish) controller, fat library, super thin model. Also, form validation is done in the library too, so any changes to formats, messages etc are all done in one place too. Also, I never echo anything from a controller. That is very bad (except in the case of an ajax controller of course). Controller example PHP Code: <?php Library example PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Model example PHP Code: <?php Although it can feel a bit long winded in the first draft, it gets really super quick for updates and changes and development. I am not certain this is the 'best' way but the beauty of CI is it is very forgiving allowing you to work however you feel is best. This way is the way I currently prefer for my sites. Hope that helps, Paul. PS Forgot to mention, suppose I needed to check permissions in the customer library, I would call the permissions_library to check the permissions, so again any permissions changes can all be done in one place, not needing to refer to every call to permissions dotted around. I have no problems with libraries calling libraries at all.
PS Just realised my previous answer does not really address your OO query at all. Sorry. Left the answer in rather than deleting because it took me ages :-(
|
Welcome Guest, Not a member yet? Register Sign In |