So today a friend is helping me with a PHP project I am making. He looked over my code and told me a few things to fix, etc. There was one thing he said which I don’t quite understand. He said “get your controllers out of your views and then get your models out of your controllers”. For instance he pointed out this code here which is in my website memberlist page:
$get_users = mysql_query("SELECT * FROM users") or die(mysql_error());
while($row = mysql_fetch_array($get_users)) {
$username = ucfirst($row['username']);
$user_group = getUserGroup($row['userlevel']);
$dor = $row['dor'];
print("<tr>
<td class='border-bottom'>$username</td>
<td class='border-bottom'>$user_group</td>
<td class='border-bottom'>$dor</td>
</tr>");
}
Could someone possibly explain what they mean’t about that? Could you also provide some resources if there are any.
He’s referring to what’s called MVC, or Model View Controller.
The Controller class handles the request, the Model handles calculation and compuation (usually, database connections and communication), and then it (the Controller) loads a View with the info gathered in the Model, and display that to the user.
What he means is that you should separate your logic from your display.
MVC is a great design pattern for larger applications. But if your application doesn’t have many pages, and/or is small, you don’t need it.