As I said in the segment that you just read, to make the previous user controller class more functional, it's desirable to enable it to create new users and update existing ones. To do so, I'm going to append to the controller a couple of extra methods, which will use the API of the model to perform the aforementioned tasks. Below I listed the signatures of these brand new methods, so look at them, please: // 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(); } // 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); } Understanding how these methods work is pretty straightforward. In the first case, the "create()" method is the most interesting. It uses a view object to render an HTML form, which will be used for collecting data about a new user. This logically implies that there must be another method that actually saves this information to the database, but this topic will be covered in more detail in the next tutorial. On the other hand, as its name suggests, "update()" will simply update an existing user based on its ID, which must be passed as part of the user request. It's that simple, really. After adding the previous methods, the user controller definitively looks much more useful, bur the most important thing to note here is the way that it reuses some of the classes of the framework under the schema of the MVC design pattern. Now that you've grasped the underlying logic of the controller, it's time to show its full source code, including the couple of methods that you just learned. This will be done in the last section of this tutorial, so to get there simply click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|