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!
blog comments powered by Disqus |
|
|
|
|
|
|
|