Frame/Frame.class.php
<?php
//声明命名空间
namespace Frame;//定义最终的框架初始类
final class Frame
{//公共的静态的框架初始化方法public static function run(){self::initConfig(); //初始化配置数据self::initRoute(); //初始化路由参数self::initConst(); //初始化常量定义self::initAutoLoad(); //初始化类的自动加载self::initDispatch(); //初始化请求分发}//私有的静态的初始化配置信息private static function initConfig(){//开启SESSION会话session_start();$GLOBALS['config'] = require_once(APP_PATH."Conf".DS."Config.php");}//私有的静态的初始化路由参数private static function initRoute(){//获取路由参数$p = $GLOBALS['config']['default_platform']; //平台参数$c = isset($_GET['c']) ? $_GET['c'] : $GLOBALS['config']['default_controller']; //控制器名$a = isset($_GET['a']) ? $_GET['a'] : $GLOBALS['config']['default_action']; //动作名define("PLAT",$p);define("CONTROLLER",$c);define("ACTION",$a);}//私有的静态的初始化目录常量private static function initConst(){define("VIEW_PATH",APP_PATH."View".DS.CONTROLLER.DS); //View目录}//私有的静态的初始化类的自动加载private static function initAutoLoad(){//类的自动加载spl_autoload_register(function($className){//传递过来类名参数:Home\Controller\StudentController//类文件的真实路径:./Home/Controller/StudentController.class.php//将传递的类名转成真实类文件路径$filename = ROOT_PATH.str_replace("\\",DS,$className).".class.php";//如果类文件存在,则包含if(file_exists($filename)) require_once($filename);}); }//私有的静态的初始化请求分发:创建哪个控制器类的对象?调用控制器对象的哪个方法?private static function initDispatch(){//构建控制器类名称:Home\Controller\StudentController$controllerClassName = PLAT."\\"."Controller"."\\".CONTROLLER . "Controller";//创建控制器类的对象$controllerObj = new $controllerClassName();//根据用户的不同动作,调用不同的方法$action_name = ACTION;$controllerObj->$action_name();}
}
Frame/Libs/BaseController.class.php
<?php
//声明命名空间
namespace Frame\Libs;//定义抽象的基础控制器类
abstract class BaseController
{//受保护的Smarty对象属性protected $smarty = null;//构造方法public function __construct(){//创建Smarty类的对象$smarty = new \Frame\Vendor\Smarty();//配置Smarty的左右定界符$smarty->left_delimiter = "<{";$smarty->right_delimiter = "}>";//指定新的编译目录:c:\windows\temp\view\$smarty->setCompileDir(sys_get_temp_dir().DS.'view_c'.DS);//指定视图目录:./Home/View/Student/$smarty->setTemplateDir(VIEW_PATH);//给$this->smarty属性赋值$this->smarty = $smarty;}//用户登录判断protected function denyAccess(){//判断用户是否存在if(empty($_SESSION['username'])){$this->jump("请先登录!","?c=User&a=login");} }//跳转方法protected function jump($message,$url='?',$time=3){echo "<h2>{$message}</h2>";header("refresh:{$time};url={$url}");die();}
}
Frame/Libs/BaseModel.class.php
<?php
//声明命名空间
namespace Frame\Libs;//定义抽象的基础模型类
abstract class BaseModel
{//受保护的PDO对象属性protected $pdo = null;//构造方法public function __construct(){//创建PDOWrapper类的对象$this->pdo = new \Frame\Vendor\PDOWrapper();}//公共的静态的创建不同模型类对象的方法public static function getInstance(){//获取静态化方式调用的类名$modelClassName = get_called_class();//创建指定模型类对象,并返回return new $modelClassName();}//获取一行数据public function fetchOne($where="2>1"){//构建查询的SQL语句$sql = "SELECT * FROM {$this->table} WHERE {$where}";//执行SQL语句,返回一维数组return $this->pdo->fetchOne($sql);}//获取多行数据public function fetchAll(){//构建查询的SQL语句$sql = "SELECT * FROM {$this->table} ORDER BY id DESC";//执行SQL语句,返回二维数组return $this->pdo->fetchAll($sql);}//删除记录public function delete($id){//构建删除的SQL语句$sql = "DELETE FROM {$this->table} WHERE id={$id}";//执行SQL语句,并返回布尔值return $this->pdo->exec($sql);}//添加记录public function insert($data){//构建"字段列表"和"值列表"字符串$fields = '';$values = '';foreach($data as $key=>$value){$fields .= "$key,";$values .= "'$value',";}//去除结尾的逗号$fields = rtrim($fields,',');$values = rtrim($values,',');//构建插入的SQL语句:INSERT INTO news(title,content,hits) VALUES('标题','内容','30')$sql = "INSERT INTO {$this->table}($fields) VALUES($values)";//执行SQL语句,并返回布尔值return $this->pdo->exec($sql);}//更新记录public function update($data,$id){//构建"字段名=字段值"的字符串$str = "";foreach($data as $key=>$value){$str .= "{$key}='{$value}',";}//去除结尾的逗号$str = rtrim($str,',');//构建更新的SQL语句:UPDATE news SET title='标题',content='内容' WHERE id=5$sql = "UPDATE {$this->table} SET {$str} WHERE id={$id}";//执行SQL语句,并返回布尔值return $this->pdo->exec($sql);}//获取记录数public function rowCount($where="2>1"){//构建查询的SQL语句$sql = "SELECT * FROM {$this->table} WHERE {$where}";//执行SQL语句,并返回记录数return $this->pdo->rowCount($sql);}
}
\Frame\Vendor\PDOWrapper
<?php
//声明命名空间
namespace Frame\Vendor;
use \PDO;
use \PDOException;//定义最终的PDOWrapper类
final class PDOWrapper
{//数据库配置信息private $db_type; //数据库类型private $db_host; //主机名private $db_port; //端口号private $db_user; //用户名private $db_pass; //密码private $db_name; //数据库名private $charset; //字符集private $pdo = null;//公共的构造方法public function __construct(){$this->db_type = $GLOBALS['config']['db_type'];$this->db_host = $GLOBALS['config']['db_host'];$this->db_port = $GLOBALS['config']['db_port'];$this->db_user = $GLOBALS['config']['db_user'];$this->db_pass = $GLOBALS['config']['db_pass'];$this->db_name = $GLOBALS['config']['db_name'];$this->charset = $GLOBALS['config']['charset'];$this->createPDO(); //创建PDO对象$this->setErrMode(); //设置报错模式}//私有的创建PDO对象的方法private function createPDO(){try{//构建DSN字符串$dsn = "{$this->db_type}:host={$this->db_host};port={$this->db_port};";$dsn .= "dbname={$this->db_name};charset={$this->charset}";//创建PDO类的对象$this->pdo = new PDO($dsn, $this->db_user, $this->db_pass);}catch(PDOException $e){echo "<h2>创建PDO对象失败!</h2>";echo "错误编号:".$e->getCode();echo "<br>错误行号:".$e->getLine();echo "<br>错误文件:".$e->getFile();echo "<br>错误信息:".$e->getMessage();die();}}//私有的设置PDO报错模式private function setErrMode(){$this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);}//执行SQL语句:insert、update、delete、set等public function exec($sql){try{return $this->pdo->exec($sql);}catch(PDOException $e){$this->showErrMsg($e);}}//获取单行数据:SELECT * FROM studentpublic function fetchOne($sql){try{//执行SQL语句,并返回结果集对象PDOStatement$PDOStatement = $this->pdo->query($sql);//返回一条记录return $PDOStatement->fetch(PDO::FETCH_ASSOC);}catch(PDOException $e){$this->showErrMsg($e);}}//获取多行数据:SELECT * FROM studentpublic function fetchAll($sql){try{//执行SQL语句,并返回结果集对象PDOStatement$PDOStatement = $this->pdo->query($sql);//返回多条记录return $PDOStatement->fetchAll(PDO::FETCH_ASSOC);}catch(PDOException $e){$this->showErrMsg($e);}}//获取记录数public function rowCount($sql){try{//执行SQL语句,并返回结果集对象PDOStatement$PDOStatement = $this->pdo->query($sql);//返回记录数return $PDOStatement->rowCount();}catch(PDOException $e){$this->showErrMsg($e);} }//显示错误信息private function showErrMsg($e){echo "<h2>执行SQL语句失败!SQL语句有问题!</h2>";echo "错误编号:".$e->getCode();echo "<br>错误行号:".$e->getLine();echo "<br>错误文件:".$e->getFile();echo "<br>错误信息:".$e->getMessage();die();}
}
\Frame\Vendor\Smarty
<?php
//声明命名空间
namespace Frame\Vendor;//包含原始的Smarty类文件
require_once(ROOT_PATH."Frame".DS."Vendor".DS."Smarty".DS."libs".DS."Smarty.class.php");//定义自己的Smarty类,并继承原始的Smarty类
final class Smarty extends \Smarty
{//升级和扩展的代码
}
\Frame\Vendor\Captcha
<?php
//声明命名空间
namespace Frame\Vendor;//定义最终的验证码类
final class Captcha
{//私有的成员属性private $codelen; //验证码长度private $code; //验证码字符串private $width; //图片的宽度private $height; //图片的高度private $fontsize; //字号大小private $fontfile; //字体文件private $img; //图像资源//公共的构造方法public function __construct($codelen=4,$width=85,$height=22,$fontsize=18){$this->codelen = $codelen;$this->width = $width;$this->height = $height;$this->fontsize = $fontsize;//字体文件必须是绝对路径$this->fontfile = ROOT_PATH."Public".DS."Admin".DS."Images".DS."msyh.ttf"; $this->createCode(); //生成随机的验证码字符串$this->createImg(); //创建画布$this->createBg(); //画布背景$this->createFont(); //写入文本$this->outPut(); //输出图像}//私有的生成随机字符串的方法private function createCode(){$arr1 = array_merge(range('a','z'),range(0,9),range('A','Z'));shuffle($arr1); //打乱数组$arr2 = array_rand($arr1,4); //随机取4个下标$str = "";foreach($arr2 as $index){$str .= $arr1[$index];}$this->code = $str;}//私有的创建画布的方法private function createImg(){$this->img = imagecreatetruecolor($this->width,$this->height);}//私有的绘制画布背景方法private function createBg(){//分配颜色$color1 = imagecolorallocate($this->img,mt_rand(200,250),mt_rand(200,250),mt_rand(200,255));//绘制带背景的矩形imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color1);//绘制像数点for($i=1;$i<=200;$i++){$color3 = imagecolorallocate($this->img,mt_rand(0,250),mt_rand(0,250),mt_rand(50,255));imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color3);}//绘制线段for($i=1;$i<10;$i++){$color4 = imagecolorallocate($this->img,mt_rand(0,250),mt_rand(0,250),mt_rand(50,255));imageline ($this->img ,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color4);}}//私有的写入文本到图像上private function createFont(){$color2 = imagecolorallocate($this->img,mt_rand(0,250),mt_rand(0,250),mt_rand(50,255)); imagettftext($this->img,$this->fontsize,0,10,20,$color2,$this->fontfile,$this->code);}//私有的输出图像private function outPut(){header("content-type:image/png");imagepng($this->img);imagedestroy($this->img);}//公共的获取验证码字符串的方法public function getCode(){return strtolower($this->code);}
}