| |||||||||||
| |||||||||||
Model, View, Controller, Components, Framework and Helpers - single file
MVC, 4 layer model and templating example from a Well House Consultants training course
More on MVC, 4 layer model and templating [link]
Source code: mvc_principles.php Module: H302
<?php
/* This example illustrates the move from an "all in one" web page towards an architecture where the look and feel is split off from the computation and data logic, with intermediate stages being provided either as PHP code by the application programmer, or as standard code from a common library. Let's name each of the elements ... * the look and feel (template for the page) - the VIEW * the computation and data logic - the MODEL * the intermediate code that connects the model and view - the CONTROLLER * standard all-encompassing code that sits above the page - a FRAMEWORK / Scaffold * standard functions that are called by the page - FRAMEWORK / Components * user code that's in the form of reused functions - HELPERS */ #---------- Representative of the FRAMEWORK / Scaffold ............. error_reporting(E_ALL ^ E_NOTICE); date_default_timezone_set("Europe/London"); session_start(); #---------- HELPER functions (may be framework components in some systems) function starttable() { return "<table border=1>"; } function endtable() { return "</table>"; } function startrow() { return "<tr>"; } function endrow() { return "</tr>"; } function cell($content) { return "<td>".say($content)."</td>"; } function say($summat) { return htmlspecialchars($summat); } #---------- Representative of the FRAMEWORK / Components ............. function sendToOutput($fillers,$template) { // $html = file_get_contents($template); # line above is what we will do when the view is in a separate file # line below is a temporary fix for this single-file example! global $template; $html = $template; foreach (array_keys($fillers) as $fillme) { $html = preg_replace("/%$fillme%/",$fillers[$fillme],$html); } print ($html); } #---------- MODEL - data stuff that's nothing to do with the web use function getstats($name = "Worth") { $about = file("stats.txt"); $rv = array(); foreach ($about as $lyne) { $parts = explode("\t",$lyne); if (preg_match("/$name/",$parts[6])) { $rv[$parts[6]] = $parts[12]; } } return $rv; } #--------- VIEW - the template to which each page is produced $template = <<<THE_VIEW <html> <head><title>MVC from first principles - Zoned code</title></head> <body> <h1>All about you and me ;-)</h1> %table% <hr /> © Me and you Ltd, %year% </body> </html> THE_VIEW; # -------------------------------------------------------------- # HERE'S WHERE TO START MAKING USE OF THE ABOVE - the CONTROLLER # -------------------------------------------------------------- // Run model code $results = getstats("London"); // Feed results into view-populating object $fill["year"] = date("Y"); $table = starttable(); 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,"mylookandfeel.htp"); ?> Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in PHP * PHP Programming * PHP Techniques * Intermediate PHP - weekend course / hobby / club / leisure users * PHP Programming * Learning to program in PHP Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering PHP are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "MVC, 4 layer model and templating" training module. You'll find a description of the topic and some
other closely related examples on the "MVC, 4 layer model and templating" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |