HomePHP Page 3 - Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP
Setting up the basics for generating disparate views: defining a controller based on MySQL datasets - PHP
Are you a curious PHP developer, wanting to learn how to simulate a basic Model-View-Controller schema with PHP? Then, look no further, because you’re in the right place. Welcome to the final part of the series “Simulating the Model-View-Controller Schema in PHP.” In three tutorials, this series provides you with a comprehensive introduction, aimed at implementing a MVC-based relationship between PHP objects.
Since I plan to implement an MVC-based schema by using native MySQL result sets, obviously one of the first things I have to do is create a result set controller, which can be directly represented by a PHP class. The first link of this programming chain is the class below, which I called "ResultController," and its definition is as follows:
// define 'ResultController' (controller) class ResultController{ private $outputRanges=array('xhtml','xml','plain'); private $output; public function __construct($output='xhtml'){ if(!in_array($output,$this->outputRanges)){ throw new Exception('Invalid result set output!'); } $this->output=$output; } public function getOutput(){ return $this->output; } }
As you can see, this new "ResultController" class is responsible for instructing the corresponding model (keep in mind again that the model is represented by a MySQL result set) about what type of output should be generated. In this case, the class in question will handle three specific kind of views for rendering: PLAIN, XHTML, and XML respectively, thus any other type of output will be refused by this class.
Like the examples shown in the previous two articles of this series, the above class also exposes an accessor method (the "getOutput()" method), which comes in very handy for retrieving the respective output selected by the controller. Still with me? Good, now that you know how this entirely new controller class looks, let me show you the next class that composes the MVC schema.
Yes, you're correct! In the next few lines I'll define the model class, which not surprisingly will be represented by a MySQL dataset wrapper. Therefore, keep reading to learn how this class will be coded.