Home arrow PHP arrow Page 2 - 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 - 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.

  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



Since I plan to follow a logical sequence for building this new MVC schema with PHP, first I’ll define the controller itself, which in this case is represented by a simple web page controller class.

As you’ll learn later on, this class will be responsible for instructing the model about what style sheet should be used when rendering the corresponding web document. But, in fact, I’m getting ahead of myself, so now pay attention to the definition of this controller class, which is listed below:

// define 'PageController' class (controller)
class PageController{
    private $styleRanges=array('styles1','styles2','styles3');
    private $style;
    public function __construct($style='styles1'){
            throw new Exception('Invalid web page style!');
    // return type of view
    public function getStyle(){
        return $this->style;

As shown above, the “PageController” class accepts the name of a specific style sheet as the only incoming argument, in order to assign it as a property inside the respective constructor. Of course, it’s easy to guess that this style will be used by the controller class to instruct the web document on what style sheet to display, by using the “getStyle()” method defined above.

Still with me? Fine, now that you know how the web page controller class looks, let me walk one step further and show you the second link of the MVC schema. In the next section of the article, you’ll see how to build a simple –- yet efficient -- web page generator class, which will represent the model element inside the MVC relationship that I’m currently developing.

To learn more about how this class will be defined, please click on the link that appears below and keep on reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: