Zend Framework 教程:简单的注册和登录验证

注:本系列教程都在zend studio 6.1.1上开发完成,amp环境全部自己配置,以避免使用类似xampp\wamp等继承环境带来的未知问题。

1、首先创建存储用户的表

image

 

2、创建对应于数据库表的数据库访问模型,即Zend Table

在zend studio的工程上右键选择New Zend Framework Item,选择Zend Table,使用Users作为PHP文件名,以下是Users.php的内容:

image

代码具体的含义不言自明。

3、创建控制器

使用同2的方法创建Zend Framework Item,不过选择的是Zend Controller,使用AuthController.php作为PHP文件名。内容如下(控制器中使用的LoginForm 和RegistrationForm 是Zend_Form的子类,用于创建表单,细节在下一节):

<?php
         /** * AuthController *
          * @author happydagui.min
          * @version */
          require_once 'Zend/Controller/Action.php';
          require_once '../application/default/forms/LoginForm.php'; // 注意这里的路径的写法 require_once '../application/default/forms/RegistrationForm.php';
          class AuthController extends Zend_Controller_Action {
                /** * The default action - show the home page */
                public function homeAction() { // 进入首页前检查用户是否已经登录,否则转向登录页面
                      $storage = new Zend_Auth_Storage_Session ( );
                      $data = $storage->read ();
                      if (! $data) { $this-&gt;_redirect ( 'auth/login' ); }
                      $this-&gt;view-&gt;username = $data-&gt;username;
                       }
                public function loginAction() { // 处理登录
                     $users = new Users ( ); $form = new LoginForm ( ); $this-&gt;view-&gt;form = $form; if ($this-&gt;getRequest ()-&gt;isPost ()) { if ($form-&gt;isValid ( $_POST )) { $data = $form-&gt;getValues (); $auth = Zend_Auth::getInstance (); $authAdapter = new Zend_Auth_Adapter_DbTable ( $users-&gt;getAdapter (), 'tb_users' ); $authAdapter-&gt;setIdentityColumn ( 'username' ) -&gt;setCredentialColumn ( 'password' ); $authAdapter-&gt;setIdentity ( $data ['username'] ) -&gt;setCredential ( $data ['password'] ); $result = $auth-&gt;authenticate ( $authAdapter ); if ($result-&gt;isValid ()) { $storage = new Zend_Auth_Storage_Session ( ); $storage-&gt;write ( $authAdapter-&gt;getResultRowObject () ); $this-&gt;_redirect ( 'auth/home' ); } else { $this-&gt;view-&gt;errorMessage = "Invalid username or password, Please try again."; } } } } public function signupAction() { // 处理注册 $users = new Users ( ); $form = new RegistrationForm ( ); $this-&gt;view-&gt;form = $form; if ($this-&gt;getRequest ()-&gt;isPost ()) { if ($form-&gt;isValid ( $_POST )) { $data = $form-&gt;getValues (); if ($data ['password'] != $data ['confirmPassword']) { $this-&gt;view-&gt;errorMessage = "Password and confirm password donnot match."; return; } if ($users-&gt;checkUnique ( $data ['username'] )) { $this-&gt;view-&gt;errorMessage = "Name already taken."; return; } unset ( $data ['confirmPassword'] ); $users-&gt;insert ( $data ); $this-&gt;_redirect ( 'auth/login' ); } } } public function logoutAction() { // 处理注销 $storage = new Zend_Auth_Storage_Session ( ); $storage-&gt;clear (); $this-&gt;_redirect ( 'auth/login' ); } }
// 这里去掉了php的结束符号 4、显示注册和登录的表单类,需要手工创建LoginForm.php(位置:application/default/forms/)
image
RegistrationForm.php(位置:application/default/forms/)
image
 
5、创建完控制器和表单后,下一步自然是视图
home.phtml
 
image

login.phtml文件

image

signup.phtml文件

image 

如有什么问题,请提出来探讨。我这里完全可以运行。

另外,可以考虑抽象控制器的一个基类,来实现统一的验证。这个后续再将。

原文地址:https://www.cnblogs.com/buffer/p/2115700.html