HomePHP 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.
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'){ if(!in_array($style,$this->styleRanges)){ throw new Exception('Invalid web page style!'); } $this->style=$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.