PHP
  Home arrow PHP arrow Page 5 - 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 - 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! 



     
     
    >>> 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 3 Hosted by Hostway
    Stay green...Green IT