Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/4066_MVC ... n-PHP.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
MVC and Frameworks - a lesson from first principles in PHP

PHP 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 view

And 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.
(written 2013-04-19, updated 2013-04-20)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
  [2199] Improving the structure of your early PHP programs - (2009-05-25)
  [2612] The Model, View, Controller architecture (MVC) - what, why and how. - (2010-02-01)
  [3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [3705] Django Training Courses - UK - (2012-04-23)
  [3919] What is a web framework? - (2012-11-10)
  [4010] Really Simple Rails - (2013-02-17)
  [4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
  [4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
  [4391] Refactoring Perl applications to give them a rosy future - (2015-01-11)
  [4527] Hello Flask world / Python web micro framework - (2015-10-11)
  [4641] Using an MVC structure - even without a formal framework - (2016-02-07)
  [4691] Real life PHP application using our course training MVC example - (2016-06-05)

H302 - PHP - MVC, 4 layer model and templating
  [1634] Kiss and Book - (2008-05-07)
  [1716] Larger applications in PHP - (2008-07-22)
  [1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
  [2174] Application design in PHP - multiple step processes - (2009-05-11)
  [2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
  [3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
  [3956] Zend / layout of MVC and other files in an example application (PHP) - (2012-12-16)
  [4314] PHP training - refreshed modern course, backed up by years of practical experience - (2014-11-16)


Back to
Handling requests to a forum - the background process
Previous and next
or
Horse's mouth home
Forward to
The woman, the television, the bullock and Darlington
Some other Articles
Passing variable between PHP pages - hidden fields, cookies and sessions
Even early on, separate out your program from your HTML!
Arrays in PHP - contain different and even mixed data types
The woman, the television, the bullock and Darlington
MVC and Frameworks - a lesson from first principles in PHP
Handling requests to a forum - the background process
Apache httpd - a robust, open source web server
Backups by crossover between network centres - setting up automatic scp transfers
Sessions, forms and validation in CodeIgniter - early examples
Seamless, integrated IT - we have a long way to go!
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4066_MVC ... n-PHP.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb