CodeIgniter Forums
best practice separating business logic from controller and model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: best practice separating business logic from controller and model (/showthread.php?tid=78155)



best practice separating business logic from controller and model - tekrda - 12-06-2020

Hi Eeveryone,
is there any best practice in CI4 to manage a separate layer for business logic which has model references and can be used in controllers so that redundancy can be minimized and have a better code organization?

thanks


RE: best practice separating business logic from controller and model - captain-sensible - 12-06-2020

its been 6 hours no answer so let me kick it off : i have a feelig that Lonnie new book might answer this:

https://leanpub.com/codeigniter4foundations

also depends on complexity. The most basic pattern is :

view : presentation and what web surfer sees. Can be made more compact. Instead of a "header" view , "content" view and "footer" view, that make up web page just use "layout".
model: connects to and models data
controller : basic logic

so for instance i use PHPMailer in a controller the lines are not excessive so i could user a "manager class" which controller will use but from a birds eye view , it probably won't reduce code.Then Lonnie talks about dependency injection, entities and so on. I think what you find works best for you is the way; tidying up code and reducing stuff will flow naturally , at least it does in my case .


RE: best practice separating business logic from controller and model - blagi - 12-07-2020

(12-06-2020, 11:40 AM)captain-sensible Wrote: its been 6 hours no answer so let me kick it off : i have a feelig that Lonnie new book might answer this:

https://leanpub.com/codeigniter4foundations

also depends on complexity. The most basic pattern is :

view : presentation and what web surfer sees. Can be made more compact. Instead of a "header" view , "content" view and "footer" view, that make up web page just use "layout". 
model: connects to and models data
controller : basic logic

so for instance i use PHPMailer in a controller the lines are not excessive so i could user a "manager class" which controller will use but from a birds eye view , it probably won't reduce code.Then Lonnie talks about dependency injection, entities and so on. I think what you find works best for you is the way; tidying up code and reducing stuff will flow naturally , at least it does in my case .

Business logic should be in model, separated from DB model. Business logic should be omitted from view and controller. For CI4 structure probably the best place for business logic is Domain folder under app folder.
I'm not expert at that field but worked in a company that was trying to develop using Domain Driven Design (DDD) approach.