Over the last few weeks I have been studying the MVC design pattern for web applications using PHP. Taking a broad view I understand how the pattern works and why it is a very good way of implementing any sort of web application from small or large.
As I understand it we have 3 distinct layers that talk to each other via the Controller like so:
User Input ---> View ---> Controller ---> Model
Site Output <--- View <--- Controller <--- Model
With my planned implementation I hope to have a model for each table in my database, and each model will have all the functions/logic necessary to manage this table. In turn each of these models will have an associated view. The Model and View will of course have a Controller which allows them to be used.
Now this is easy to get my head around as each single, logical action requiring the database has been covered. However, what happens when a particular action requires the use of more than one table/model?
The Admin side of the application isn’t likely to need more than one model at a time to maintain the database. The front-end or user side of the application is a different matter! Say I have a webpage that shows a list of articles for a particular section, a list of currently logged-in users and – borrowing an example from SO – site statistics such as a tag cloud.
This one page would require at least 3 models in my planned design – Article, Users and Tags.
Obviously my single controllers aren’t going to cut it. So what do I do?
Create new monolithic controllers for my web pages?
- allow me to get the results I want
- would require a lot of duplicate coding
- really hard to maintain if changes required
Create a “super” controller that manipulates smaller specific controllers
- allows me to get the results I want
- would be modular so changes to one script shouldn’t affect the others
- minimal code duplication
Create [insert brilliant solution here]
I am currently erring towards Option 2. Simply because it should in theory cut down on coding as all the necessary behaviour will exist in the smaller controllers - and everything will be easy to maintain.
Perhaps it could look like this:
articlecontroller.php
<?php
//Article Controller Script
if($_GET['article'] = 'foo')
{
//magic necessary for displaying article "foo".
}
?>
usercontroller.php
<?php
//User Controller Script
if($_GET['user'] = 'display')
{
//magic necessary for displaying users
}
?>
supercontroller.php
<?php
//"Super" Controller
//magic for setting up page
if(isset($_GET['article']))
{
include('articlecontroller.php');
}
if(isset($_GET['user']))
{
include('usercontroller.php');
}
?>
As you can see, my super controller looks at what that particular page requires and includes the necessary controllers which do the grunt work. NB: The code is just a quick and dirty example :)
However, I am by no means a pro so that is why I am asking you, SO. Which option is preferred? If neither what Option 3 would you suggest? Any code snippets/examples would be lovely but not required.
If you made it this far, thank you for your time.