Zend Framework2 会员管理项目之五: 添加用户注册功能
2014年03月16日
我们分以下步骤完成用户注册功能
1. 添加user模块,与zf2官方文档一样,添加User.php, UserTable.php
2. 添加user表单以及表单验证,RegisterForm.php与RegisterFilter.php
3. 在Module.php getServiceConfig函数注册上述表单
4. 在UserController.php中添加register函数实现注册
5. 添加register.phtml视图文件,完成注册界面
在module/Member/src/Member/Model中添加User.php与UserTable.php
User.php, 简单的类文件,
exchangeArray()主要是将用户注册的数据(数组)转化为User的成员。
setPassword()将密码加密,这里采用sha1方式,也可以用md5等加密方式。
<?php
/**
* This source file is part of Qiai.
*
* PHP Version >=5.3
*
* @category Qiai_Application
* @package Member
* @subpackage Model
* @author Sai (QIAI) <sai@qiais.com>
* @license Qiai-License http://www.qiai.com/licenses/qiai.ck.html
* @link http://www.qiais.com
*/
namespace Member\Model;
/**
* User model
*
* @category Gc_Application
* @package Member
* @subpackage Model
*/
class User
{
public $id;
public $department_id;
public $name;
public $email;
public $password;
public $role;
public $tel;
public $fax;
public $mobile;
public $zipcode;
public $address;
public $created;
/**
* set password
*
* @param string $plain_password plain key
*
* @return void
*/
public function setPassword($plain_password)
{
$this->password = sha1($plain_password);
}
/**
* change form data to members of Member
*
* @param array $data form data
*
* @return void
*/
function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->department_id = (isset($data['department_id'])) ? $data['department_id'] : null;
$this->name = (isset($data['name'])) ? $data['name'] : null;
$this->email = (isset($data['email'])) ? $data['email'] : null;
$this->role = (isset($data['role'])) ? $data['role'] : null;
$this->tel = (isset($data['tel'])) ? $data['tel'] : null;
$this->mobile = (isset($data['mobile'])) ? $data['mobile'] : null;
$this->fax = (isset($data['fax'])) ? $data['fax'] : null;
$this->zipcode = (isset($data['zipcode'])) ? $data['zipcode'] : null;
$this->address = (isset($data['address'])) ? $data['address'] : null;
$this->created = (isset($data['created'])) ? $data['created'] : null;
if (isset($data["password"])) {
$this->setPassword($data["password"]);
}
}
/**
* get object data
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
}
UserTable.php
saveUser()函数将用户数据保存到数据库,判断是否有id,没有的话为新注册,有的话为更新。
<?php
/**
* This source file is part of Qiai.
*
* PHP Version >=5.3
*
* @category Qiai_Application
* @package Member
* @subpackage Model
* @author Sai (QIAI) <sai@qiais.com>
* @license Qiai-License http://www.qiai.com/licenses/qiai.ck.html
* @link http://www.qiais.com
*/
namespace Member\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
/**
* Member: TableGateway
*
* @category Gc_Application
* @package Member
* @subpackage Model
*/
class UserTable
{
/**
* TableGateway
*/
protected $tableGateway;
/**
* __construct
*
* @param TableGateway $tableGateway TableGateway
*
* @return void
*/
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
/**
* fetchAll: get all data from table member.
*
* @return ResultSet
*/
public function fetchAll($paginated=false)
{
if ($paginated) {
// create a new Select object for the table album
$select = new Select('users');
// create a new result set based on the Album entity
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
// create a new pagination adapter object
$paginatorAdapter = new DbSelect(
// our configured select object
$select,
// the adapter to run it against
$this->tableGateway->getAdapter(),
// the result set to hydrate
$resultSetPrototype
);
$paginator = new Paginator($paginatorAdapter);
return $paginator;
}
$resultSet = $this->tableGateway->select();
return $resultSet;
}
/**
* saveMember: save data for user.
*
* @param Member $user user object
*
* @return ResultSet
*/
public function saveUser(User $user)
{
$nowtime = date("Y-m-d H:i:s");
$data = array(
'email' => $user->email,
'name' => $user->name,
'role' => $user->role,
'password' => $user->password,
'tel' => $user->tel,
'mobile' => $user->mobile,
'fax' => $user->fax,
'zipcode' => $user->zipcode,
'address' => $user->address,
'modified' => $nowtime
);
$id = (int)$user->id;
if ($id == 0) {
$data['created'] = $nowtime;
$this->tableGateway->insert($data);
} else {
if ($this->getUser($id)) {
if(empty($data['password']))
unset($data['password']);
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('User ID does not exist');
}
}
}
/**
* Get User account by user id
*
* @param string $id string
*
* @throws \Exception
* @return Row
*/
public function getUser($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
/**
* deleteMember: delete user.
*
* @param int $id user id
*
* @return void
*/
public function deleteUser($id)
{
$this->tableGateway->delete(array('id' => $id));
}
/**
* getUserByEmail: get user by email.
*
* @param string $email email address
*
* @return Object
*/
public function getUserByEmail($email)
{
$rowset = $this->tableGateway->select(array('email' => $email));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $email");
}
return $row;
}
}
待续…..