September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Managing User Accounts with the Zend Framework

  • November 16, 2009
  • By Jason Gilmore
  • Send Email »
  • More Articles »

The Account Model

Next you'll need to create the account model pursuant to the typical convention defined by the Zend Framework. I'm going to keep the initial model capabilities to a minimum, although we'll add to it as necessary.

class Default_Model_Customer extends Zend_Db_Table_Abstract
{
  protected $_name = 'customer';
}

The Account Registration Form

Next up is the registration form. I'm going to keep this to the minimum required fields, creating the form you see in Figure 1. As there's nothing special about this form, following the screenshot I'll skip the code and move on to the controller action used to process this form information.

Zend Framework Registration Form
Figure 1. A simple user registration form

Processing the User Registration

With the account table, model, and registration form in place, it's time to write the code which will validate the user's registration information and create the account. For the purposes of this exercise presume the the form action points to an action named register created within a controller named account:

  01 public function registerAction()
  02 {   
  03   // Create array for errors
  04   $this->view->errors = array();
  05 
  06   if ($this->getRequest()->isPost()) {
  07 
  08     $customer = new Default_Model_Customer();
  09 
  10     $result = $customer->save($this->_request->getPost());
  11   
  12     if (! is_array($result)) {
  13    
  14       $this->view->created = 1;                     
  15     
  16     } else {
  17   
  18       $this->view->errors = $result;
  19    
  20       $this->view->firstName     = $this->_request->getPost('first-name');
  21       $this->view->lastName      = $this->_request->getPost('last-name');
  22       $this->view->email         = $this->_request->getPost('email');
  23       $this->view->pswd          = $this->_request->getPost('pswd'); 
  24       $this->view->userName      = $this->_request->getPost('username'); 
  25   
  26     }     
  27 
  28   }
  29   
  30 }

Let's review some of the stickier parts of this action:

  • Line 04 initializes an array which will contain any errors triggered throughout the data validation process.
  • If the registration form has been submitted, line 06 will detect the POSTed data, setting the validation and account creation process into motion.
  • Line 10 attempts to create the user account by calling a method named save(), which resides in the Account model. This method contains all of the validation and insertion logic, allowing us to maintain a thin controller containing remarkably little code. I'll show you what the save() method looks like in a moment. For the moment just keep in mind that if an array is returned from the method call, one or more errors occurred, so we'll assign them to a view variable (line 18) and populate some variables for redisplay in the form. Otherwise we'll create a view variable named $this->view->created which will serve as an indicator to inform the user of a successfully created account.

The account model's save() method looks like this:

  01 public function save(array $data)
  02 {
  03  
  04  // Initialize the errors array
  05  $errors = array();
  06  
  07  // First Name
  08  if (! Zend_Validate::is($data['first-name'], 'NotEmpty')) {
  09      $errors[] = "Please provide your first name.";
  10  }
  11 
  12  // Last Name
  13  if (! Zend_Validate::is($data['last-name'], 'NotEmpty')) {
  14      $errors[] = "Please provide your last name.";
  15  }     
  16 
  17  // Does Email already exist?
  18  if (Zend_Validate::is($data['email'], 'EmailAddress')) {
  19 
  20    $result = $this->findByEmail($data['email']);
  21   
  22    if ($result != false) {
  23      $errors[] = "An account using this e-mail address already exists.";
  24    }
  25  
  26  } else {
  27    $errors[] = "Please provide a valid e-mail address.";
  28  }
  29  
  30  // Password must be at least 6 characters
  31  $valid_pswd = new Zend_Validate_StringLength(6,20);
  32  if (! $valid_pswd->isValid($data['pswd'])) {
  33      $errors[] = "Your password must be 6-20 characters.";
  34  }
  35  
  36  // If no errors, insert the 
  37  if (count($errors) == 0) {
  38   
  39    $data = array (
  40      'first_name'         => $data['first-name'],
  41      'last_name'          => $data['last-name'],
  42      'email'              => $data['email'],
  43      'pswd'               => md5($data['pswd']),         
  44      'recovery_key'       => ''      
  45    );
  46   
  47    return $this->insert($data);
  48 
  49  } else {
  50    return $errors;
  51  }
  52  
  53 }

All of this should look pretty straightforward, so I won't dive into a line-by-line breakdown, other than to say that the findByEmail() is a simple method found in the model which verifies that an account using the provided e-mail address doesn't already exist.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel