To better understand how the performance of the MySQL-driven program developed in the previous chapter of the series can be improved by means of the cache class, it’s necessary to list the program’s full source files as they were initially created. Below I included the definitions of those files, starting with the user controller class. Here it is: (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 As shown above, the user controller class implements a few basic methods for retrieving all of the users stored in the corresponding MySQL table, and for inserting, updating and deleting records in that table. In keeping with the schema followed by the MVC design pattern, these operations are executed by the model; the view class is responsible for sending the generated output to the browser. Speaking of browser output, below is the pair of view files that perform this task. First, look at the view that displays the full list of users: (users.php) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <?php if (empty($users)):?> <h2>Oops! There are not users in the database.</h2> <?php else:?> <?php foreach($users as $user):?> <p><strong>First Name : </strong><?php echo $user->fname;?></p> <p><strong>Last Name : </strong><?php echo $user->lname;?></p> <p><strong>Email : </strong><?php echo $user->email;?></p> <hr /> <?php endforeach?> <?php endif?> </body> </html> And here’s the second view. It renders the HTML form required for inserting new users into the database table: (user_form.php) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <?php echo Form::open(array('action' => 'save', 'method' => 'post'));?> <p>First Name: <?php echo Form::input(array('type' => 'text', 'name' => 'fname'));?></p> <p>Last Name: <?php echo Form::input(array('type' => 'text', 'name' => 'lname'));?></p> <p>Email: <?php echo Form::input(array('type' => 'text', 'name' => 'email'));?></p> <p><?php echo Form::input(array('type' => 'submit', 'name' => 'send', 'value' => 'Create user'));?></p> <?php echo Form::close();?> </body> </html> With the inclusion of these two view files, it’s easy to understand how this example web program works. Naturally, its functionality relies on most of the classes that comprise the MVC framework, which you hopefully learned about in previous tutorials. However, as I pointed out in the introduction, the framework’s cache class has been left abandoned (if the term is really applicable in this case), off in a dark corner. This is about to change. In the following segment I’m going to demonstrate how to exploit its functionality to avoid performing additional SQL queries when fetching users from the database. This process will be discussed in detail in the section to come. So click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|