MVC and Frameworks - a lesson from first principles in PHP
Archive - Originally posted on "The Horse's Mouth" - 2013-04-19 23:33:48 - Graham EllisPHP is a nice, easy entry level embedded tagging system - an "HTML++" language in which programmers and web site developers can drop elements of code into a web page and perform simple and straigtforward tasks very quickly. But the downside of that is that the resultant file very quickly becomes a confusing mixture of HTML, program logic that's associated with the application's data, and programming logic that's associated with the formatting of that data for web display. As a control case, I've coded an example showing those issues [here].
By splitting the code into a series of areas, we can make the whole thing much more followable and easy to debug, even though it will be longer. The areas are:
• the VIEW - the HTML itself, with very limited markup to indicate where data is to be inserted
• the MODEL - the PHP code that deals purely with the data and calculations and logic thereon
• the FRAMEWORK - surrounding code that starts (bookstraps) the whole processing of the page
• HELPERS - common routines that may come standard with the framework, or may be ones you write and
• the CONTROLLER - the PHP code that ties all the other elements together
You'll see all of these elements of code [here] - that's a conversion of the example just above into a single file in which each of them is in its own section of the file.
But a view may want to be shared by a number of controllers, and the same model used in several applications. Framework code will be shared widely, and there will be a commonality in helpers - and so you'll want to take each of these elements out into a separate file for separate maintainance, and for sharing on a different pattern. The result is a file that contains just the controller, and which references the other files. There's an example [here] ... the code looks like this:
# load in the framework and framework components, and bootstrap the application
include ("z_framework.php");
# load in the user's helpers
include ("z_helpers.php");
# load in the user's model classes
include ("z_model.php");
// Run model code
$results = getstats("Manchester"); // Running model code
// Feed results into view-populating object
$fill["year"] = date("Y");
$table = starttable(); // using helpers
foreach (array_keys($results) as $paramName) {
$table .= startrow();
$table .= cell($paramName);
$table .= cell($results[$paramName]);
$table .= endrow();
}
$fill["table"] = $table . endtable();
// Populate and render the view
sendToOutput($fill,"z_view.htp");
// Using the Framework to render the viewAnd each of the other files is straightforward:
The framework and components [here]
The helpers [here]
The model code [here]
and the view template [here]
This example was written during a private PHP course over the last few days, and will now be used on some of our public PHP courses to help people understand the why and how of "MVC" and Frameworks.