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