Recommended OO approach when using CI |
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. |
Messages In This Thread |
Recommended OO approach when using CI - by CINewb - 05-11-2016, 05:16 PM
RE: Recommended OO approach when using CI - by PaulD - 05-12-2016, 03:42 PM
RE: Recommended OO approach when using CI - by PaulD - 05-12-2016, 04:06 PM
|