Interface接口的設置是由其餘模塊決定的php
描述:我寫了個登錄器 無非就是登錄和註冊這2個功能,固然還有一個驗證碼功能mysql
根據職責性,我登錄器不涉及任何用戶有關操做。sql
但我須要規定用戶模型必須有如下幾個功能,我登錄器才能使用,登錄、註冊、根據名字獲取用戶的我的信息。cookie
因此我設置以下session
<?php interface IMember { /** * 根據用戶名獲取用戶信息 用戶名不指定能夠是郵箱、暱稱或者手機號碼 * @param String $username */ public function getOneMemberByUsername($username); /** * 登錄檢測 */ public function login($user); /** * 新增用戶 */ public function register($user); }
有了這個規定以後,個人登錄器就能夠寫了ui
<?php /** * WCMS 登錄器 只跟用戶有關注冊、登錄有關 其餘無關 判斷有無登錄 能夠設置cookie * 描述 調用了MemberService指定接口 login register getOneMemberByUsername * @author wolf * @since 2014-08-02 * @version 第4次簡化 * */ class AnonymousController extends Action { const COOKIENAME = 'user'; /** * 管理員登陸口 */ public function admin() { if (isset ( $_COOKIE [COOKIENAME] )) { $userInfo = self::getMemberService ()->getOneMemberByUsername ( $_COOKIE [COOKIENAME] ); $this->view ()->assign ( 'user', $userInfo ); $this->view ()->assign ( "userinfo", $userInfo ); } $this->view ()->display ( 'file:anonymous/login.tpl' ); } /** * 用戶註冊 接口 調用用戶服務 * 只檢測提交的字段是否合法 * @todo 默認用戶組未添加 */ public function register() { $group = self::getMemberService ()->getMemberGroup (); $this->view ()->assign ( "group", $group ); $this->view ()->display ( 'mysql:register.tpl' ); } /** * 用戶提交註冊 */ public function setRegister() { $rs = self::getMemberService ()->register ( $_POST ); $this->sendNotice ( $rs, null, false ); } /** * 普通會員登陸 */ public function login() { //導入推薦用戶 $this->loginExtend (); if (isset ( $_COOKIE [COOKIENAME] )) { $userInfo = self::getMemberService ()->getOneMemberByUsername ( $_COOKIE [COOKIENAME] ); $this->view ()->assign ( 'user', $userInfo ); $this->view ()->assign ( "userinfo", $userInfo ); } $this->view ()->display ( 'mysql:login.tpl' ); } /** * 登陸擴展信息 DIY */ private function loginExtend() { $userface = self::getMemberService ()->getRandFace ( 12 ); $this->view ()->assign ( 'face', $userface ); } /** * 生成驗證碼 */ public function captcha() { require 'Captcha.php'; //確保每次都能取到新值 unset ( $_SESSION ['authum_session'] ); // 實例化一個對象 $_vc = new Captcha (); $_vc->doimg (); $_SESSION ['authnum_session'] = $_vc->getCode (); } /** * 登陸驗證 */ public function setLogin() { $rs = self::getMemberService ()->login ( $_POST ); //登錄成功 默認記錄8個小時 if ($rs == SUCCESS) { setcookie ( COOKIENAME, $_POST ['username'], time () + 3600 * 10, "/" ); } $this->sendNotice ( $rs, null, false ); } /** * 退出登陸 同步登陸 */ public function signout() { setcookie ( COOKIENAME, "", - 86400, "/" ); $this->redirect ( "退出成功!", '/anonymous/login' ); } /** * 獲取用戶服務類 */ public static function getMemberService() { return new MemberService (); } }