PHP
  Home arrow PHP arrow Page 5 - 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 - Putting the MVC schema to work: generating style sheets on the fly


    (Page 5 of 5 )

    In order to demonstrate how the MVC schema can be used to attach different style sheets to the web page created by the respective “WebPage” class, first I’ll define three sample style files. As you’ll see, these styles will be displayed dynamically, based on the instructions given originally by the "PageController" class.

    In consonance with the concepts that I deployed before, here are the respective definitions of the three sample CSS files:

    “styles1.css” file

    body{
        margin: 0;
        padding: 0;
        background: #fff;
    }
    h1 {
        font: bold 22px Arial, Tahoma;
        color: #000;
        margin: 0;
    }
    #header{
        height: 100px;
        background: #9c0;
    }
    #navbar{
        background: #cfc;
        padding: 4px 0 4px 20%;
    }
    #navbar ul{                  
        display: inline;
    }
    #navbar li{
        list-style-type: none;    
        display: inline;
    }
    #navbar a, #navbar a:visited{ 
        display: block;
        float: left;
        width: 100px;
        height: 15px;
        padding: 2px 0 0 0;
        margin-right: 4px;
        font: normal 11px Tahoma, Arial;
        color: #000;
        text-align: center;
        text-decoration: none;
        border: 1px solid #000;
    }
    #navbar a:hover{
        background: #fff;
    }
    #leftcol{
        float: left;
        width: 20%;
        height: 500px;
        background: #9c0;
    }
    #centercol{
        float: left;
        width: 58%;
        height: 500px;
        background: #fff;
    }
    #rightcol{
        float: right;
        width: 20%;
        height: 500px;
        background: #9c0;
    }
    #footer{
        clear: left;
        height: 20px;
        background: #cfc;
    }

    “styles2.css” file

    body{
        margin: 0;
        padding: 0;
        background: #fff;
    }
    h1 {
        font: bold 22px Arial, Tahoma;
        color: #000;
        margin: 0;
    }
    #header{
        height: 100px;
        background: #f60;
    }
    #navbar{
        background: #cfc;
        padding: 4px 0 4px 20%;
    }
    #navbar ul{
        display: inline;
    }
    #navbar li{
        list-style-type: none;
        display: inline;
    }
    #navbar a, #navbar a:visited{
        display: block;
        float: left;
        width: 100px;
        height: 15px;
        padding: 2px 0 0 0;
        margin-right: 4px;
        font: normal 11px Tahoma, Arial;
        color: #000;
        text-align: center;
        text-decoration: none;
        border: 1px solid #000;
    }
    #navbar a:hover{
        background: #fff;
    }
    #leftcol{
        float: left;
        width: 20%;
        height: 500px;
        background: #f60;
    }
    #centercol{
        float: left;
        width: 58%;
        height: 500px;
        background: #fff;
    }
    #rightcol{
        float: right;
        width: 20%;
        height: 500px;
        background: #f60;
    }
    #footer{
        clear: left;
        height: 20px;
        background: #cfc;
    }

    “styles3.css” file

    body{
        margin: 0;
        padding: 0;
        background: #fff;
    }
    h1 {
        font: bold 22px Arial, Tahoma;
        color: #000;
        margin: 0;
    }
    #header{
        height: 100px;
        background: #9cf;
    }
    #navbar{
        background: #cfc;
        padding: 4px 0 4px 20%;
    }
    #navbar ul{
        display: inline;
    }
    #navbar li{
        list-style-type: none;
        display: inline;
    }
    #navbar a, #navbar a:visited{
        display: block;
        float: left;
        width: 100px;
        height: 15px;
        padding: 2px 0 0 0;
        margin-right: 4px;
        font: normal 11px Tahoma, Arial;
        color: #000;
        text-align: center;
        text-decoration: none;
        border: 1px solid #000;
    }
    #navbar a:hover{
        background: #fff;
    }
    #leftcol{
        float: left;
        width: 20%;
        height: 500px;
        background: #9cf;
    }
    #centercol{
        float: left;
        width: 58%;
        height: 500px;
        background: #fff;
    }
    #rightcol{
        float: right;
        width: 20%;
        height: 500px;
        background: #9cf;
    }
    #footer{
        clear: left;
        height: 20px;
        background: #cfc;
    }

    All right, after defining the three CSS files that you saw before, take a look at the following example, which uses the first style file, and then renders the web page in question:

    // display web page with the first style sheet
    try{
        $pageController=new PageController('styles1');
        $webPage=new WebPage($pageController);
        $webPage->doHeader();
        $webPage->doBody();
        $webPage->doFooter();
        $styleGenerator=new StyleGenerator($webPage);
        echo $styleGenerator->generateStyle();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    As you can see, the above script shows how the “PageController” object determines what style sheet to use when rendering the corresponding web page. The result of this code snippet can be appreciated in the screen shot below:

    Similarly, the other two style sheets can be attached to the web document as shown below:

    // display web page with the second style sheet
    try{
        $pageController=new PageController('styles2');
        $webPage=new WebPage($pageController);
        $webPage->doHeader();
        $webPage->doBody();
        $webPage->doFooter();
        $styleGenerator=new StyleGenerator($webPage);
        echo $styleGenerator->generateStyle();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    // display web page with the third style sheet

    try{
        $pageController=new PageController('styles3');
        $webPage=new WebPage($pageController);
        $webPage->doHeader();
        $webPage->doBody();
        $webPage->doFooter();
        $styleGenerator=new StyleGenerator($webPage);
        echo $styleGenerator->generateStyle();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    Definitely, you’ll have to agree with me that this simple MVC schema really works. Try creating more style sheets and see what happens in each case. The experience is really worthwhile!

    Final thoughts

    Unfortunately, we’re finished now. Over this second part of this series, I demonstrated how to implement the Model-View-Controller schema with PHP, by setting up a simple example that shows how to attach different style sheets to a given web document, based on the instructions given by a page controller.

    If this example wasn’t enough for you, there’s still more material to learn. In the last tutorial, I’ll show you how to use an MVC-based object relationship to produce disparate outputs from returned MySQL result sets. See you in the last part! 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 2 hosted by Hostway
    Stay green...Green IT