Where Should Elements Go Using MVC Format Using CodeIgniter
-
19-09-2019 - |
Question
This is my first foray into using an MVC construct (CodeIgniter). I'm hoping someone can tell me where the following elements belong. I have them written; I just want to make sure they're placed properly in their respective locations. This is how my application will run:
- Call a DB and see if we have a user signed up
- Route to a signup page
- Route to the main preferences page for existing users
- Make DB queries for producing a new user
- Make update queries when users change their preferences
The service being provided is a cron job cycling every 10 minutes, which I still have written outside of CodeIgniter. Is this something I should/could add to the logic somewhere? It pings Twitter, and does stuff with any new tweets. Let me know if I can clarify any part of this!
Solution
- Model
- Controller
- Controller
- Model
- Model
Rule of thumb: if it involves the database or the state of the application, it belongs in a model. If it is HTML or presentation logic, it belongs in a view. Controllers handle the rest of the logic, and help link the views and models together.
OTHER TIPS
There are a lot of other things that come up too:
- Where should I sanitise data? As it comes from the model - in the controller or finally before I view? I do it generally in the view if it's something like
htmlspecialchars()
(though I'm sure others might disagree).