PHP
  Home arrow PHP arrow Page 3 - Building a Web Page Controller for Simulating the Model-View-Controller Schema in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building a Web Page Controller for Simulating the Model-View-Controller Schema in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2006-08-14


    Table of Contents:
  • Building a Web Page Controller for Simulating the Model-View-Controller Schema in PHP
  • Creating the first component of the schema: defining a web page controller class
  • Creating a real-world model: defining a web page generator class
  • Completing the MVC schema: defining a style sheet generator class
  • Putting the MVC schema to work: generating style sheets on the fly

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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
    ( Page 3 of 5 )

    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){
            $this->pageController=$pageController;
            $this->controllerStyle=$pageController->getStyle();
        }
        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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT