MVC for those who might have forgotten
I’ve recently stumbled upon some oddities on a classic MVC framework.
Queries done from the View, Redirecting from the View, HTML formatting in the Controller, it was all pretty messy.
So just to refresh everyone’s mind (and for those who might’ve tried to mess around with an MVC framework without learning the concept), this is an example of what should be on each of them:
Model Example - User.php - representing an entity/functions that involve the table User.
It would probably include functions like is_user_active() (for the an object of class User), todays_users() (static function) , get_admins() (static function) and so on.
You can prepare the structure of the data here (arrays, objects, sortation [via extra parameters], and so on). This should be the ONLY place where Database queries are done, don’t make ANY queries outside the Model.
manipulation of data is done here.
Controller Example - main.php - holds the functions for most of the website’s view,
for instance: all_members - a controller showing all users except admins, retrieves those with the function User::get_members() (or $this->User->get_members(), same thing).
There shouldn't be too much messing around with the data structure (leave it to the Model, seriously), minor changes are ok, form validation is perfect, finally - send the data to the View.
no manipulation of data is done here.
View Example - all_members.php - HTML (sometimes one of various Template languages),
this is basically the page itself, getting most of the information from the Controller, it’s mostly a matter of displaying it.
You can have some loops, and some logic.
no manipulation of data is done here either (duh!).
I hope this clarified some basic things about MVC, because I keep seeing those mistakes over and over again.
Keep your Model Fat, and Controller Slim, and if a video would be more helpful to illustrate it: