主要:
MVC目錄結構
數據庫工具類制作
創建公共模型類和公共控制器類
--------------:--------------------------------------├─index.php 入口文件├─Model 模型│ └─UserModel.html' target='_blank'>class.php 用戶模型類├─View 視圖│ └─login.html 登錄表單頁面├─Controller 控制器│ └─UserController.class.php 用戶控制器├─Frame 公共使用的類│ ├─BaseModel.class.php 數據庫連接類│ ├─BaseController.class.php 控制器公共操作(設置編碼,信息跳轉)│ └─Db.class.php 數據庫操作工具類└─Public 靜態公共文件(js,css,images) ├─js/ js文件 ├─css/ css樣式文件 └─images img圖片-----------------------------------------------------------------MVC目錄結構
1)準備: 創建分支
1 $ git checkout master2 $ git checkout -b mvc-dbtools-base
2) 創建目錄結構:
MVC目錄: Model/ Controller/ View/
靜態資源目錄: Public/
3) 創建項目入口文件 【index.php】
拷貝原login.php的編碼header(...)
引入創建的控制器UserController 和 模型 UserModel
1 ?php 2 /** 3 * 入口文件 4 */ 5 header( content-type:text/html;charset=utf-8 7 require_once( Model/UserModel.class.php 8 require_once Controller/UserController.class.php 10 //實例化控制器 11 $userCtr = new UserController(); 13 $a = !empty($_GET[ a ]) ? $_GET[ a ] : login 15 $userCtr - $a();
4) 創建控制器UserController 【Controller/UserController.class.php】
1 ?php 2 /** 3 * UserController.class.php 用戶控制器 4 */ 6 class UserController { 7 /** 8 * 展示登錄界面 9 * @access public 10 */ 11 public function login() 12 { 13 include View/login.html 14 } 16 /** 17 * 登錄操作: 校驗登錄信息 18 */ 19 public function dlogin() 20 { 21 //接收登錄信息 22 $data = array(); 23 $data[ username ] = trim($_POST[ username 24 $data[ pwd ] = trim($_POST[ pwd 26 //實例化模型,調用模型方法,校驗用戶名和密碼 27 $model = new UserModel(); 28 $result = $model- checkLoginInfo($data); 30 //結果提示信息 31 if($result){ 32 exit( 登錄成功 33 } else { 34 echo 用戶名或密碼不正確! 35 header( refresh:3; url=? 36 } 37 } 38 }
5) 創建用戶模型類UserModel 【Model/UserModel.class.php】
實現方法:checkLoginInfo() 檢驗用戶名和密碼
?php/** * UserModel.class.php * 用戶模型類-操作表pbg_users */class UserModel{ /** * 檢驗登錄信息 * @param array $data 用戶提交的登錄信息 * @return bool true-校驗成功 false-校驗失敗 */ public function checkLoginInfo($data) { //連接數據庫 $conn = @mysql_connect( localhost , root , root ) or die( 連接數據庫失敗! mysql_query( set names utf8 ,$conn); mysql_query( use web ,$conn); //查詢數據庫 $sql = select username,pwd from pbg_users where username= {$data[ username ]} $res = mysql_query($sql,$conn); //登錄結果提示信息 if($res !== false){ $user = mysql_fetch_array($res); if( $user[ pwd ] == md5($data[ pwd ]) ){ return true; } return false;}
查看用戶模型類
6)登錄視圖:【view/login.html】
引入路徑的修正,
表單提交action修正: action=?a=dlogin
1 !DOCTYPE html 2 html lang= zh-CN 3 head 4 meta charset= UTF-8 5 title 登錄 /title 6 link rel= stylesheet type= text/css href= public/layui/css/layui.css 7 link rel= stylesheet type= text/css href= public/css/style.css 8 /head 9 body 10 p >
點擊查看登錄視圖源碼
數據庫連接操作都在模型中,可以提取出來制作數據庫操作工具類
數據庫工具類制作數據庫連接,設置編碼,選擇數據庫
實現單例
獲取一行數據getOneRow, 獲取單個數組 getOneData, 獲取多行getAllRows, 增刪改 exec
自動生成insert 或 update語句 autoExecute()
【Frame/Db.class.php】
1 ?php 2 /** 3 * Db.class.php 數據庫操作工具類 4 * @author young 5 */ 7 class Db 8 { 9 private $host; //主機 10 private $user; //用戶名 11 private $pwd; //密碼 12 private $port; //端口 13 private $charset; //編碼 14 private $dbname; //數據庫 16 private $link=null; //存儲數據庫資源 17 private static $instance=null; //存儲本類實例對象 19 /** 20 * 構造方法: 保存數據庫連接信息,連接數據庫 21 * @access private 22 * @param array $conf 數據庫連接信息 23 */ 24 private function __construct($conf) 25 { 26 $this- host = !empty($conf[ host ]) ? $conf[ host ] : localhost 27 $this- user = !empty($conf[ user ]) ? $conf[ user ] : root 28 $this- pwd = !empty($conf[ pwd ]) ? $conf[ pwd ] : root 29 $this- port = !empty($conf[ port ]) ? $conf[ port ] : 3306 30 $this- charset = !empty($conf[ charset ]) ? $conf[ charset ] : utf8 31 $this- dbname = !empty($conf[ dbname ]) ? $conf[ dbname ] : web 33 $this- connect(); 34 } 36 /** 37 * 連接數據庫,設置編碼,選庫 38 * @access private 39 */ 40 private function connect() 41 { 42 $this- link = @ mysql_connect( {$this- host}:{$this- port} , $this- user , $this- pwd ) or die( 連接失敗! .mysql_error()); 43 $this- setCharset($this- charset); 44 $this- useDb($this- dbname); 45 } 46 /** 47 * 設置字符便 48 * @access public 49 * @param string $char 字符編碼 50 */ 51 public function setCharset($char) 52 { 53 $this- query( set names $char 54 } 55 /** 56 * 選擇數據庫 57 * @access public 58 * @param string $dbname 數據庫名稱 59 */ 60 public function useDb($dbname) 61 { 62 $this- query( use $dbname 63 } 65 /** 66 * 執行sql語句 67 * @param string $sql sql語句 68 * @return mixed 69 */ 70 private function query($sql) 71 { 72 $result = mysql_query($sql, $this- link); 73 if(false === $result) { 74 echo p sql執行失敗! br 75 echo br 失敗語句: .$sql; 76 echo br 錯誤代號 .mysql_errno(); 77 echo br 錯誤提示: .mysql_error(). /p 78 exit(); 79 } 80 return $result; 81 } 83 /** 84 * 獲取本類實例 85 * @access public 86 * @param array $conf 數據庫連接信息 87 * @return object 本類的單例對象 88 */ 89 public static function getDb($conf) 90 { 91 if(false === (static::$instance instanceof static)){ 92 static::$instance = new static($conf); 93 } 94 return static::$instance; 95 } 96 /** 97 * 禁止克隆 98 */ 99 public function __clone() 100 { 101 102 } 103 /** 104 * 關閉數據庫連接 105 * @access public 106 */ 107 public function closeDb() 108 { 109 mysql_close($this- link); 110 } 111 112 public function exec($sql) 113 { 114 $result = $this- query($sql); 115 return $this- affectedRows(); 116 117 } 118 /** 119 * 受影響的行數 120 * @return int 返回受影響的行數 121 */ 122 private function affectedRows() 123 { 124 return mysql_affected_rows($this- link); 125 } 126 127 /** 128 * 執行 “返回一行數據”的查詢 129 * @param string $sql sql語句 130 * @return array 一維數組(一行) 131 */ 132 public function getOneRow($sql) 133 { 134 $result = $this- query($sql); 135 $data = mysql_fetch_assoc($result); 136 mysql_free_result($result); 137 return $data; 138 } 139 /** 140 * 執行 返回多行數據 的查詢 141 * @param string $sql sql語句 142 * @return array 二維數組 143 */ 144 public function getAllRows($sql) 145 { 146 $result = $this- query($sql); 147 $data = array(); 148 while($row = mysql_fetch_assoc($result)){ 149 $data[] = $row; 150 } 151 mysql_free_result($result); 152 return $data; 153 } 154 /** 155 * 執行“獲取一個數據”的查詢 156 * @param string $sql sql語句 157 * @return mixed 標量數據值 158 */ 159 public function getOneData($sql) 160 { 161 $result = $this- query($sql); 162 $data = mysql_fetch_row($result); 163 mysql_free_result($result); 164 return $data[0]; 165 } 166 167 /** 168 * 上次insert時的自增長id值 169 * @return int insert時的id值 170 */ 171 public function getInsertId() 172 { 173 return mysql_insert_id($this- link); 174 } 175 176 /** 177 * 序列化時,對指定數據進行序列化 178 * @return array 指定進行序列化的數據 179 */ 180 public function __sleep() 181 { 182 return array( host , port , user , pass , charset , dbname 183 } 184 /** 185 * 反序列化時,使用相應數據連接數據庫 186 */ 187 public function __wakeup() 188 { 189 $this- connect(); //連接數據庫 190 } 191 /** 192 * 自動生成insert語句或update語句 193 * @param array $data insert或update的數據 194 * @param string $table 操作的數據表 195 * @param string $act 是update還是insert操作 196 * @param string $where where條件 如 id=2 如果是update必須加,否則不執行直接返回false 197 * @return bool 執行insert與update的結果 198 */ 199 public function autoExecute($data, $table, $act= insert , $where= ) 200 { 201 if($act == insert ) { 202 $sql = insert into .$table. ( 203 $sql .=implode( , , array_keys($data)); 204 $sql .= ) values ( 205 $sql .= implode( , , array_values($data)); 206 $sql .= ) 207 208 $res = $this- exec($sql); 209 return $this- getInsertId(); 210 211 } else if($act == update ) { 212 if(!$where) { return false; } 213 $sql = update .$table. set 214 foreach ($data as $k = $v) { 215 $sql .= $k. = .$v. , 216 } 217 $sql = substr($sql, 0, -1); 218 $sql .= where .$where; 219 220 return $this- exec($sql); 221 } else { 222 return false; 223 } 224 225 } 226 }點擊查看工具類
創建公共模型類和公共控制器類1) 公共模型類【Frame/BaseModel.class.php】 獲取數據庫操作工具類實例
1 ?php 3 /** 4 * BaseModel.class.php 基礎模型類 5 * 連接數據庫 6 */ 7 class BaseModel 8 { 9 protected $db = null; 10 /** 11 * 構造方法: 實例化數據庫類 12 * @access public13 * @param array $config 數據庫配置信息 14 */ 15 function __construct(array $config=null) 16 { 17 $conf = array( 18 host = localhost , 19 user = root , 20 pwd = root , 21 port = 3306 , 22 charset = utf8 , 23 dbname = web , 24 ); 25 $conf = empty($config)? $conf : array_merge($conf,$config); 26 $this- db = Db::getDb($conf); 27 } 28 }2) 公共控制器類【Frame/BaseController】 :
統一了編碼
提示信息跳轉
1 ?php 2 /** 3 * BaseController.class.php 公共控制器 4 * @author young 5 */ 7 class BaseController 8 { 9 /** 10 * 統一編碼utf8 11 */ 12 public function __construct() 13 { 14 header( content-type:text/html;charset=utf-8 15 session_start(); 16 } 18 /** 19 * 跳轉提示 20 * @access public 21 * @param string $msg 跳轉提示信息 22 * @param string $url 跳轉的地址 23 * @param integer $time 等待時間 秒數 24 */ 25 public function msg($msg= , $url= ? , $time=3) 26 { 27 echo p a href= $url 返回 /a /p 頁面將在{$time}秒之后跳轉?。? 28 header( refresh: $time; url=$url 29 exit( p span >3)入口文件中引入工具類,基礎模型和基礎控制器類
【index.php】
1 ?php 2 /** 3 * 入口文件 4 */ 5 require_once Frame/Db.class.php 6 require_once Frame/BaseModel.class.php 7 require_once( Model/UserModel.class.php 8 require_once Frame/BaseController.class.php 9 require_once Controller/UserController.class.php 11 //實例化控制器 12 $userCtr = new UserController(); 14 $a = !empty($_GET[ a ]) ? $_GET[ a ] : login 16 $userCtr - $a();點擊查看入口文件
4)用戶模型類優化 【Model/UserModel.class.php】
1 ?php 3 /** 4 * UserModel.class.php 5 * 用戶模型類-操作表pbg_users 6 */ 7 class UserModel extends BaseModel 8 { 9 /** 10 * 檢驗登錄信息 11 * @param array $data 用戶提交的登錄信息 12 * @return bool true-校驗成功 false-校驗失敗 13 */ 14 public function checkLoginInfo($data) 15 { 16 $sql = select id,username,pwd from pbg_users where username= {$data[ username ]} 17 $res = $this- db- getOneRow($sql); 18 return $res[ pwd ] == md5($data[ pwd ]) ? : false; 19 } 20 }5) 用戶控制器登錄操作,跳轉提示優化
使用公共控制器方法msg()
1 。。。。。。。 2 /** 3 * 登錄操作: 校驗登錄信息 4 */ 5 public function dlogin() 6 { 7 //接收登錄信息 8 $data = array(); 9 $data[ username ] = trim($_POST[ username 10 $data[ pwd ] = trim($_POST[ pwd 12 //實例化模型,調用模型方法 13 $model = new UserModel(); 14 $result = $model- checkLoginInfo($data); 15 //跳轉提示 16 if($result){ 17 $this- msg( 登錄成功! , ?a=index ,3); 18 } else { 19 $this- msg( 用戶名或密碼不正確!! 20 } 21 }合并保存并推送git
1 $ git add -A2 $ git commit -m MVC結構,數據庫操作類,基礎模型類和基礎控制器類制作 3 $ git checkout master4 $ git merge mvc-dbtools-base5 $ git push origin master小結: 優化目錄結構,制作數據庫操作類,基礎模型類與基礎控制器類使用
下一步: 模型類實現單例, 進一步優化目錄結構,區分平臺(如前臺,后臺)
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
相關推薦:
淺談PHP源碼五:關于array 數組的創建
淺談PHP源碼一:explode和implode函數
以上就是php源碼之實現單入口MVC結構的方法的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答