![]() |
When to put code in Controller and when in Model - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: When to put code in Controller and when in Model (/showthread.php?tid=86237) |
When to put code in Controller and when in Model - Rinart - 01-21-2023 MVC pattern seems quite clear but sometimes I'm not sure where certain things should go - controller or model. Examples that feel clear enough:
RE: When to put code in Controller and when in Model - kenjis - 01-21-2023 (01-21-2023, 10:42 AM)Rinart Wrote: You can move the logic to: - a library class - parent controller class - a trait RE: When to put code in Controller and when in Model - Rinart - 01-21-2023 (01-21-2023, 07:02 PM)kenjis Wrote:(01-21-2023, 10:42 AM)Rinart Wrote: Would it be a bad idea to move logic into an Entity? Like this: PHP Code: // App/Entities/Order.php RE: When to put code in Controller and when in Model - kenjis - 01-21-2023 An entity represents a database record. I think it should not know about File Uploading. RE: When to put code in Controller and when in Model - ozornick - 01-22-2023 Create a new layer in folder Services, Helpers...: class OrderService or class OrderCreator Add a method to it with your desired parameters: public function createOrderFromUser($user, $order, $file, $options, ...) and inside you work with several models (namely with a database), upload a picture, check validation and so on. In response, return either true/false or a new order with a picture. In controller call: PHP Code: $orderHelper = new OrderService(); |