Home arrow PHP arrow Page 3 - Adding More Methods to the Controller Class of an MVC-Based Framework

Creating new users and updating existing ones - PHP

In this eleventh part of the series, I expand the initial functionality of the user controller class defined previously by adding two simple methods to it. These allow it to create new users in the associated MySQL table.

TABLE OF CONTENTS:
  1. Adding More Methods to the Controller Class of an MVC-Based Framework
  2. Review: the initial definition of the user controller class
  3. Creating new users and updating existing ones
  4. The full definition of the controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
June 30, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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