Home arrow PHP arrow Page 4 - Completing an MVC-Based Framework's Controller Class

The finished version of the user controller class - PHP

In this twelfth part of the series I finish building the controller class that permits you to run CRUD operations against the pertinent “users” MySQL table. This class uses the model to interface with the data layer, the input class to filter incoming data, and the view class to display output on the browser. This process demonstrates the real functionality of the MVC framework developed in previous tutorials.

TABLE OF CONTENTS:
  1. Completing an MVC-Based Framework's Controller Class
  2. Managing users via a controller class
  3. Saving and deleting user records
  4. The finished version of the user controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
July 06, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Below I listed the full source code corresponding to the user controller class, so you can have its complete definition available in one place for further analysis.

Here’s the file that contains 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

Job done. At this point, we've finished constructing the user controller class. Now, the class can perform CRUD operations against a selected database via the model’s API. It also shows in a nutshell how easy it is to create database-driven applications by simply reusing the core components of this sample MVC framework built in PHP 5.

Naturally, in keeping with the schema of the Model-View-Controller design pattern, some methods of the controller, such as “index()” and “create(),” include a pair of view files that are used for displaying user-related data and for rendering an HTML form, which is handy for inserting new users into the corresponding MySQL table.

As I stated before though, these view files will be coded in the next tutorial, so for the moment feel free to tweak all of the code samples included in this part of the series, which hopefully will keep you entertained for a while.

Final thoughts

In this twelfth part of the series I finished building the controller class that permits you to run CRUD operations against the pertinent “users” MySQL table. As you saw before, this class uses the model to interface with the data layer, the input class to filter incoming data and the view class to display output on the browser. This process that demonstrates the real functionality of the MVC framework developed in previous tutorials.

Nevertheless, to get this sample MySQL-driven application completed, it’s necessary to code the view files that will generate the output. This will be covered in the upcoming tutorial. So, now that you’ve been warned, you don’t have any excuses to miss it!  



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