Home arrow PHP arrow Page 3 - Building a Web Page Controller for Simulating the Model-View-Controller Schema in PHP

Creating a real-world model: defining a web page generator class - PHP

If you’re one of those PHP developers that want to extend your background in object-based applications, then this series might be quite attractive to you. In these three tutorials (of which this is the second), you’ll learn how to simulate a simple Model-View-Controller schema, which can be easily expanded to construct more complex relationships between the different entities, in this case applicable specifically to PHP classes.

  1. Building a Web Page Controller for Simulating the Model-View-Controller Schema in PHP
  2. Creating the first component of the schema: defining a web page controller class
  3. Creating a real-world model: defining a web page generator class
  4. Completing the MVC schema: defining a style sheet generator class
  5. Putting the MVC schema to work: generating style sheets on the fly
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
August 14, 2006

print this article



As I expressed a few lines above, once the corresponding web page generator class has been defined, logically the next step rests in creating a concrete model, from which to establish a relationship with the respective controller.

This model will be based on a web page generator class, and its signature is shown below. Please take a look:

// define 'WebPage' class (model)
class WebPage{
    private $pageController;
    private $controllerStyle;
    private $page='';
    public function __construct(PageController $pageController){
    public function doHeader(){
        $this->page='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" href="defaultstyle.css" type="text/css" /><title>Sample Web Page</title></head>';
    public function doBody(){
        $this->page.='<body><div id="header"><h1>Header section</h1></div><div id="navbar"><ul><li><a href="#" title="Link 1">Link 1</a></li><li><a href="#" title="Link 2">Link 2</a></li><li><a href="#" title="Link 3">Link 3</a></li><li><a href="#" title="Link 4">Link 4</a></li><li><a href="#" title="Link 5">Link 5</a></li><li><a href="#" title="Link 6">Link 6</a></li></ul></div><div id="leftcol"><h1>Left column</h1></div><div id="centercol"><h1>Center column</h1></div><div id="rightcol"><h1>Right column</h1></div>';
    public function doFooter(){
        $this->page.='<div id="footer"><h1>Footer section</h1></div></body></html>';
    public function getPage(){
        return $this->page;
    public function getControllerStyle(){
        return $this->controllerStyle;

If you examine the above class’ source code, then you’ll quickly understand its functionality, since its structure is quite simple. In short, this web page generator class presents some straightforward methods which are responsible for building the three typical sections that comprise a web document.

Of course, here I’m speaking of a heading section, a main container and finally a footer area, which are generated by the respective "doHeader()," "doBody()" and "doFooter()" methods. Also, you should notice how this class takes up an object of type "PageController," which is directly assigned as a new class property.

However, and leaving out for a moment the methods that I just described, you should pay attention to the relationship established between the controller class that you learned before and this one. Notice how the controller class indicates to the model (in this case, the web page generator) what style sheet should be utilized when the web document is rendered, by using the “getControllerStyle()” method.

Now, are you starting to see how simple it is to define a proper interaction between the controller and the model? I hope you are!

Well, having defined the first two primary elements of this basic web page controller schema, obviously I need to create the third component that completes this scenario. As you might have guessed, I’m talking about a new PHP class, which will use the web document’s source code to attach to it different style sheets on the fly.

Within the MVC schema, this new class should be called the “View” component, and it’s the subject of the next section. Thus, click on the link below and learn how this class will be constructed.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: