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

Completing the MVC schema: defining a style sheet 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I explained in the previous section, the “View” element included within the MVC schema will be represented by a style sheet generator class. This class will be tasked with rendering the previous web page in accordance with the type of style indicated originally by the corresponding web page controller.

Based on the above concepts, this new class is defined as follows:

// define 'StyleGenerator' class (view)
class StyleGenerator{
    private $webPage;
    public function __construct(WebPage $webPage){
        $this->webPage=$webPage;
    }
    public function generateStyle(){
        return str_replace('defaultstyle',$this->webPage-
>getControllerStyle(),$this->webPage->getPage());
    }
}

As you can see, the "StyleGenerator" class listed above also accepts the respective "WebPage" object (remember again that this is the model), and uses the "generateStyle()" method to attach a style sheet to the whole web page. Again, you should notice how the pertinent style is tied to the web document, based on the "decisions" made by the controller class.

At this stage, I feel pretty satisfied, because I’ve defined the three elements that comprise the complete MVC schema. Therefore, it’s time to put all the classes together and see how different style sheets (called "views") can be dynamically attached to the web document created by the previous "WebPage" class.

To see how this will be done, you’ll have to read the upcoming section, thus click on the following link.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: