As I promised in the previous section, here’s the list of files that compose this sample MySQL-driven application, this time including the corresponding user controller class and the pair of view files coded before. First, here’s the controller: (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 Now it’s time to show the first view file, so here it is: (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 finally, here’s the second view file; it displays an HTML form using the framework’s form helper: (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> This is it. At this point, you can see that building a basic database-driven application by using the sample MVC framework is indeed a no-brainer process that can be tackled with minor hassles. Of course, the functionality of each of its components can easily be enhanced, so go ahead and do it. It’ll be a good exercise for sharpening your programming skills. Final thoughts In this penultimate chapter of the series, I finished building a MySQL-driven application that used the functionality of the framework to perform CRUD operations on a database table populated with user-related data. Even though it's pretty basic, this example demonstrated how easy it is to build applications like the one mentioned above by simply reusing the framework’s components. And speaking of components, the only one that remains unused is the Cache class. So, in the last tutorial I’m going to show you how to use this class within the earlier sample application to cache database result sets, thus improving its performance. Don’t miss the final part!
blog comments powered by Disqus |
|
|
|
|
|
|
|