Home arrow PHP arrow Page 2 - PHP MVC Framework: the Cache Class

Review: the sample application - PHP

Welcome to the final installment of a series that shows you how to build a simple MVC-based framework in PHP 5. This series walks you in a step-by-step fashion through the development of a stack of reusable components, which can be easily put to work together under a strict MVC-based layer.

TABLE OF CONTENTS:
  1. PHP MVC Framework: the Cache Class
  2. Review: the sample application
  3. Caching database result sets
  4. The user controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 12, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: