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

Saving and deleting user records - 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

As I explained in the earlier section, to make the user controller class a bit more useful, it’s necessary to enable it to save and delete records in the “users” MySQL table. To accomplish this, I’m going to add to the class a couple of methods that will perform these tasks in a very intuitive manner.

The corresponding implementations of these methods are shown below. Take a look:

// 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));

}

}

// delete existing user

public function delete($id)

{

$this->model->delete((int)$id);

}

That was simple to code and read, right? As you can see, these new methods also use the model’s API to save newly-created users to the MySQL table, and for updating users. Quit possibly, the most relevant detail to stress here is the static calls to methods of the “Input” class (remember this one?) within the “save()” method, before inserting new data into the pertinent table.

Other than that tiny subtlety, the rest of the logic implemented by the previous methods is easy to follow. So it’s time to show what the source code of the user controller class looks like after introducing these last improvements.

To see the complete definition of the controller, click on the link below and read the following segment. 



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