Since data caching is accomplished at the model level, the definition of the controller class will remain exactly the same. However, as I said in the preceding segment, you’re free to use the cache class within the controller if you feel more comfortable doing this. In summary, the definition of the user controller will be as follows: (UsersController.php) <?php class UsersController { private $model = NULL;
// constructor public function __construct() { // store model object as property $this->model = new Model(MySQL::getInstance(array('host', 'user', 'password', 'database'))); } // 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(); } // create new user public function create() { // create view object $view = new View('user_form'); // create view properties $view->title = 'Using the MVC design pattern with PHP 5'; $view->heading = 'Create new user'; // display view echo $view->display(); }
// save user public function save() { // get POST params if (Input::post('send')) { $fname = Input::post('fname'); $lname = Input::post('lname'); $email = Input::post('email'); // save user data $this->model->save(array('fname' => $fname, 'lname' => $lname, 'email' => $email)); } } // update existing user public function update($id) { $this->model->save(array('fname' => 'My first name', 'lname' => 'My last name', 'email' => 'myemail@domain.com'), (int)$id); } // delete existing user public function delete($id) { $this->model->delete((int)$id); } }// End UsersController class At first sight, the logic implemented by each method in the above controller remains the same. Nonetheless, this isn’t completely true, because the “fetchAll()” method will cache the retrieved data for a specified period of time, according to the settings passed to the instance of the cache class. If at this point, you list all of the users stored in the corresponding database table by typing the following request: http://localhost/mvc/users/ Then, if all goes well, you should see that list neatly displayed on screen. However, if a new user is added to the database by requesting the “create()” method in the controller, like this: http://localhost/mvc/users/create Then, when you refresh the list of users, it won’t reflect any changes, since the result set has been cached by default for 60 seconds. Give a try to all these examples and feel free to introduce into them the tweaks that best suit your personal needs. Now, you have at your disposal a functional MySQL-driven application that successfully implements the MVC pattern, thanks to the functionality given by its underlying framework. Isn’t that a good thing? You bet it is! Final thoughts Sad but true, we’ve come to the end of this series. However, this journey has been instructive, as you learned how to build a basic, yet functional framework in PHP 5 that implements the Model-View-Controller pattern in a pretty strict manner. As I said at the beginning of the series, I didn't intend to reinvent the wheel here; there are many production-ready frameworks available that you can use for speeding up the development of your PHP applications. Even so, this group of tutorials should provide you with the right pointers to using the MVC pattern in an object-based context, without having to deal with complex and sometimes overwhelming guidelines. See you in the next PHP tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|