PHP
  Home arrow PHP arrow Page 3 - Building a Web Page Controller for Sim...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · The second article of this series demonstrates how to use the MVC pattern, in order...
       · It seems that you don't understand the MVC pattern well.
       · Thank you for commenting on this PHP article. I do accept your criticism, but the...
       · I wish MVC was that simple. It is not though. But I still think your article is...
       · Thank you for the comments on my PHP article, and I'm glad to know it was useful...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT