The best way to understand the underlying logic of the controller class just created is by examining its source code in its current state. With that idea in mind, below I listed the partial definition of this class, naturally including the "index()" method that you learned before. Here it is: class UsersController { private $model = NULL;
// constructor public function __construct() { // store model object as property $this->model = new Model(MySQL::getInstance(array('host', 'user', 'password', 'test'))); } // fetch and display all users public function index() { // create view object $view = new View('users'); // create view properties $view->title = 'Using the MVC design pattern with PHP 5'; $view->heading = 'User List'; $view->users = $this->model->fetchAll(); // display view echo $view->display(); } } Definitely, I must admit that the controller class is starting to look a bit more functional. It's now capable of retrieving, via the model's API, all of the records contained in the "users" MySQL table. It's not possible to display them on screen yet, however, since this process requires the creation of a view file. This additional file will be defined in the next tutorial of the series. In the meantime, feel free to tweak the code of the previous controller class, to make it slightly more useful. Final thoughts That's all for the moment. Over the course of this tenth part of the series, things became really exciting because I started building a basic MySQL-driven application whose main tasks consisted of selecting, inserting, updating and deleting records on some hypothetical users by means of the framework. Right now, the application looks pretty skeletal, since it's only comprised of the MySQL database table containing some user-related records and a controller class that implements its "index()" method, which is used for fetching all of the users stored in the table and displaying this data on the browser. Nonetheless, turning this sample application into a more functional piece of software is only a question of adding more methods to the controller class. Therefore, in the tutorial to come I'm going to do precisely that, thus enabling the controller to insert and update user-related records. My final piece of advice is simple: don't miss the upcoming article!
blog comments powered by Disqus |
|
|
|
|
|
|
|