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

The user controller class - 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.

  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



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:



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)




}// 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:


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:


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!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: