zoukankan      html  css  js  c++  java
  • MVC基础框架完整版

    录文件划分总的文件夹:

     

      录文件划分文件夹下的文件:

    引入页代码:

    <?php
    require "./framework/Framework.class.php";
    Framework::run();

    引入页的框架初始化功能类:

     1 <?php
     2 //框架初始化功能类
     3 class Framework{
     4     //定义入口方法
     5     public static function run(){
     6         //声明路径常量
     7         static::_initPath();
     8         //获取配置信息
     9         static::_initConfig();
    10         //确定分发参数
    11         static::_initParam();
    12         //当前平台相关的路径常量
    13         static::_initPlatPath();
    14         //注册自动加载
    15         static::_initAutoLoad();
    16         //请求分发
    17         static::_dispatch();
    18     }
    19     
    20     //声明路径常量
    21     static function _initPath(){
    22         //目录常量
    23         define("ROOT_PATH",getcwd()."/");
    24         define("APP_PATH",ROOT_PATH."application/");//应用程序
    25         define("FRAME_PATH",ROOT_PATH."framework/");//框架类
    26         define("CONFIG_PATH",APP_PATH."config/");//配置类
    27         define("TOOL_PATH",FRAME_PATH."tool/");//工具类
    28     }
    29     
    30     //获取配置信息
    31     static function _initConfig(){
    32         $GLOBALS["config"] = require CONFIG_PATH."application.config.php";
    33     }
    34     
    35     //确定分发参数
    36     static function _initParam(){
    37         //定义控制器常量
    38         define("PLATFORM",isset($_GET["p"])? $_GET["p"] : $GLOBALS["config"]["app"]["plat"]);
    39         //定义控制器常量
    40         define("CONTROLLER",isset($_GET["c"])? $_GET["c"] : $GLOBALS["config"][PLATFORM]["controller"]);
    41         //定义动作常量
    42         define("ACTION",isset($_GET["a"]) ? $_GET["a"] : $GLOBALS["config"][PLATFORM]["action"]);
    43     }
    44     
    45     //当前平台相关的路径常量
    46     static function _initPlatPath(){
    47         //当前平台相关的常量
    48         define("CONTROLLER_PATH",APP_PATH.PLATFORM."/controller/");
    49         define("MODEL_PATH",APP_PATH.PLATFORM."/model/");
    50         define("VIEW_PATH",APP_PATH.PLATFORM."/view/");
    51     }
    52     
    53     //注册自动加载
    54     static function _initAutoLoad(){
    55         //注册自动加载的方法
    56         spl_autoload_register(array(__CLASS__,"userAutoLoad"));
    57     }
    58     //自动加载的方法 自动载入已知类文件
    59     public static function  userAutoload($class_name){
    60         //先处理确定的(框架中的核心类)
    61         //类名与类文件映射数组
    62         $framework_class_list = array(
    63             //类名=>类文件地址
    64             "Controller" =>FRAME_PATH."Controller.class.php",
    65             "Model" =>FRAME_PATH."Model.class.php",
    66             "Factory" =>FRAME_PATH."Factory.class.php",
    67             "Newdb" =>FRAME_PATH."Newdb.class.php",
    68             "Captcha" =>TOOL_PATH."Captcha.class.php",
    69             "Upload" =>TOOL_PATH."Upload.class.php"
    70         );
    71         //加载类
    72         if(isset($framework_class_list[$class_name])){
    73             require $framework_class_list[$class_name];
    74         }else if(substr($class_name,-10) == "Controller"){
    75             require CONTROLLER_PATH.$class_name.".class.php";
    76         }else if(substr($class_name,-5) == "Model"){
    77             require MODEL_PATH.$class_name.".class.php";
    78         }
    79     }
    80     
    81     //请求分发
    82     static function _dispatch(){
    83         //组织控制器名
    84         $ControllerName = CONTROLLER."Controller";
    85         //组织方法名
    86         $Action = ACTION."Action";
    87         //实例化类的对象
    88         $controlle = new $ControllerName();
    89         //调用类的方法
    90         $controlle->$Action();
    91     }
    92 }

    基础框架类(简单的数据库类的封装):

     1 //连接数据库的类
     2 class Newdb{
     3     public $host;//主机地址
     4     public $post = 3306;//mysql连接端口
     5     public $name;//数据库账号
     6     public $pwd;//数据库密码
     7     public $dbName;//数据库名
     8     public $charSet;//字符集
     9     public $link;//数据库连接对象
    10     public $res;//数据库返回的结果集
    11     
    12     //三私一共
    13     private  static $obj;//用来存Newdb对象
    14     
    15     //构造方法 实例化对象的时候自动调用
    16     private function __construct($config = array()){
    17         $this->host = $config["host"]?$config["host"] : "localhost";
    18         $this->name = $config["name"]?$config["name"] : "root";
    19         $this->pwd = $config["pwd"]?$config["pwd"] : "";
    20         $this->dbName = $config["dbName"]?$config["dbName"] : "mysql";
    21         $this->charSet = $config["charSet"]?$config["charSet"] : "utf8";
    22         
    23         //连接数据库
    24         $this->connectMysql();
    25         //设置字符集
    26         $this->setCharSet();
    27     }
    28     
    29     //私有化克隆方法
    30     private function __clone(){}
    31     //提供公共的返回对象方法
    32      static function getInstance($config = array()){
    33         if(!isset(self::$obj)){
    34             self::$obj = new self($config);    
    35         }
    36         return self::$obj;
    37     }
    38     
    39     
    40     //连接数据库
    41     function connectMysql(){
    42         $this->link = new MySQLi($this->host,$this->name,$this->pwd,$this->dbName);
    43         !mysqli_connect_error() or die("数据库连接失败");
    44         
    45     }
    46     //设置字符集
    47     function setCharSet(){
    48         $this->link->query("set names ".$this->charSet);
    49     }
    50     
    51     //执行sql语句返回结果集
    52     function queryN($sql){
    53         $this->res = $this->link->query($sql);
    54         if(!$this->res){
    55             echo ("<br />执行失败");
    56             echo "<br />失败的sql语句为:".$sql;
    57             echo "<br />出错的信息为:".mysql_error($this->link);
    58             echo "<br />错误代号为:".mysql_errno($this->link);
    59             die;
    60         }
    61         return $this->res;    
    62     }
    63     
    64     //返回字符串
    65     function getStr($sql){
    66         $zhi = $this->queryN($sql);
    67         $arr = $zhi->fetch_all();
    68         $brr = array();
    69         foreach($arr as $v){
    70             $brr[] = implode(",",$v);
    71         }
    72         return implode("^",$brr);
    73     }
    74     //返回json
    75     function getJson($sql){
    76         $zhi = $this->queryN($sql);
    77         while($row = $zhi->fetch_assoc()){
    78             $brr[] = $row;
    79         }
    80         return json_encode($brr);
    81     }
    82     //返回关联数组
    83         function getAssoc($sql){
    84         $zhi = $this->queryN($sql);
    85         while($row = $zhi->fetch_assoc()){
    86             $brr[] = $row;
    87         }
    88         return $brr;
    89     }
    90     //返回索引数组
    91     function getAttr($sql){
    92         $zhi = $this->queryN($sql);
    93         return $zhi->fetch_all();
    94     }
    95 }

    基础框架类(基础模型类):

     1 class Model{
     2     public $dao;//代表Newdb类的对象
     3     
     4     //实例化模型对象时自动创建 Newdb对象
     5     function __construct(){
     6         //调用方法
     7         $this->_initDao();
     8     }
     9     
    10     //定义个方法初始化数据库操作类的对象
    11      function _initDao(){ 
    12         $config = $GLOBALS["config"]["db"];
    13         //$this当前类的当前对象 调取数据库类的封装
    14         $this->dao = Newdb::getInstance($config);
    15      }
    16 }

    基础框架类(单例工厂类):

     1 //单例工厂类
     2 class Factory{
     3     //返回指定类 的对象
     4     public static function M($model_name){
     5         //定义一个数组用来存各种对象
     6         static $model_list=array();
     7         //判断数组中有没有指定的对象
     8         if(!isset($model_list[$model_name])){
     9             $model_list[$model_name]=new $model_name;
    10         }
    11         //返回对象
    12         return $model_list[$model_name];
    13     }
    14 }

    基础框架类(基础控制器):

     1 //基础控制器
     2 class controller{
     3     function __construct(){
     4         $this->_initHead();
     5     }
     6     //设置字符集
     7     function _initHead(){
     8         header('Content-Type: text/html; charset=utf-8');
     9     }
    10     //跳转的注释
    11     //@param $url 目标URL
    12     //@param $info 提示信息
    13     //@param $wait 等待时间(单位秒)
    14     //@return void
    15     
    16     //页面跳转的方法
    17     function _jump($url,$info=null,$wait=3){
    18         if(is_null($info)){
    19             header("location:".$url);
    20         }else{
    21             header("Refresh:$wait; URL=$url");
    22             echo $info;
    23         }
    24         die;//终止脚本
    25     }
    26 }

    应用程序类测试文件夹视图(比赛视图):

     1 <!doctype html>
     2 <html>
     3 <head>
     4 <meta charset="utf-8">
     5 <title>比赛视图</title>
     6 </head>
     7 
     8 <body>
     9     <table>
    10         <tr>
    11             <th>t_id</th>
    12             <th>t_name</th>
    13             <th>操作</th>
    14         </tr>
    15         <?php foreach($team_list as $row) : ?>
    16             <tr>
    17                 <td><?php echo $row['t_id'];?></td>
    18                 <td><?php echo $row['t_name'];?>:<?php echo $row['t2_score'];?></td>
    19                 <td>
    20                     <a href="index.php?c=Team&a=removeTeam&id=<?php echo $row['t_id']; ?>">删除</a>
    21                 </td>
    22             </tr>
    23         <?php endForeach;?>
    24     </table>    
    25 </body>
    26 </html>

    应用程序类测试文件夹视图(比赛列表):

     1 <!-- 利用HTML代码展示数据 -->
     2 <!DOCTYPE html>
     3 <html lang="en">
     4 <head>
     5     <meta charset="UTF-8">
     6     <title>比赛列表</title>
     7 </head>
     8 
     9 <body>
    10 <table>
    11 <tr>
    12 <th>球队一</th><th>比分</th><th>球队二</th><th>时间</th>
    13 </tr>
    14 <?php foreach($match_list as $row) : ?>
    15     <tr>
    16         <td><?php echo $row['t1_name'];?></td>
    17         <td><?php echo $row['t1_score'];?>:<?php echo $row['t2_score'];?></td>
    18         <td><?php echo $row['t2_name'];?></td>
    19         <td><?php echo date('Y-m-d H:i', $row['m_time']);?></td>
    20     </tr>
    21 <?php endForeach;?>
    22 </table>    
    23 </body>
    24 </html>

    应用程序类测试文件夹模型(TeamModel.class.php):

     1 <?php
     2 //创建模型类通过继承的方式继承基础模型类
     3 class TeamModel extends Model{
     4     function getList(){
     5         $sql = "select * from team";
     6         return $this->dao->getAssoc($sql);
     7     }
     8     
     9     function del($id){
    10         $sql = "delete from team where t_id = $id";
    11         $this->dao->queryN($sql);
    12     }
    13 }

    应用程序类测试文件夹模型(MatchModel.class.php):

     1 <?php
     2 //创建模型类通过继承的方式继承基础模型类
     3 class MatchModel extends Model{
     4         //定义一个方法获取数据
     5         function getList(){
     6             $db = Newdb::getInstance($config);
     7             $sql = "select t1.t_name as t1_name, m.t1_score, m.t2_score, t2.t_name as t2_name, m.m_time from `match` as m left join `team` as t1 ON m.t1_id = t1.t_id  left join `team` as t2 ON m.t2_id=t2.t_id;";
     8             
     9             //找数据库类的对象(db)调用数据库类的方法(getAssoc($sql))
    10             return $this->dao->getAssoc($sql);
    11         }
    12 }

    应用程序类测试文件夹控制器(TeamController.class.php):

     1 <?php
     2 //队伍的控制器
     3 class TeamController extends Controller{
     4     //显示队伍列表
     5     function listAction(){
     6         //实例化对象
     7         $TeamModel = Factory::M("TeamModel");
     8         $team_list = $TeamModel->getList();
     9         
    10         //引入显示的视图模板内容(HTML,css,js)
    11         require VIEW_PATH."myView.html";
    12     }
    13     
    14     //删除队伍
    15     function removeTeamAction(){
    16         //接收删除行的id
    17         $id = $_GET["id"];
    18         //实例化对象
    19         $TeamModel = Factory::M("TeamModel");
    20         $team_list = $TeamModel->del($id);
    21         
    22         $team_list = $TeamModel->getList();
    23         //引入显示的视图模板内容(HTML,css,js)
    24         require VIEW_PATH."myView.html";
    25     }
    26 }

    应用程序类测试文件夹控制器(MatchController.class.php):

     1 <?php
     2 //比赛的控制器
     3 class MatchController extends Controller{
     4     //显示比赛列表
     5     function listAction(){
     6         //实例化对象
     7         $MatchModel = Factory::M("MatchModel");
     8         $match_list = $MatchModel->getList();
     9         
    10         //引入显示的视图模板内容(HTML,css,js)
    11         require VIEW_PATH."match_list_v.html";
    12     }
    13     
    14     //修改比赛序列表
    15     function updateAction(){
    16         echo"这是修改比赛列表的方法";
    17     }
    18 }

    应用程序类配置文件夹配置系统文件(application.config.php):

     1 <?php
     2 //配置系统文件
     3 return array(
     4     //数据库的配置
     5     "db" =>array(
     6         "host" => "localhost",
     7         "name" => "root",
     8         "pwd" => "",
     9         "dbName" => "z_0705mvc",
    10         "charset" => "utf8",
    11         "post" => 3306
    12     ),
    13     //默认平台
    14     "app" =>array(
    15         "plat" => "back"
    16     ),
    17     //后台默认控制器和方法
    18     "back" =>array(
    19         "controller" =>"Login",
    20         "action" =>"login"
    21     ),
    22     //前端默认控制器和方法
    23     "front" =>array(
    24         "controller" =>"Login",
    25         "action" =>"login"
    26     ),
    27     //测试默认控制器和方法
    28     "test" =>array(
    29         "controller" =>"Match",
    30         "action" =>"list"
    31     ),
    32 );

    应用程序类后台文件夹模板(UserModel.class.php):

     1 <?php
     2 //用户表的模型类
     3 class UserModel extends Model{
     4     //登录验证的方法
     5     function check($uid,$pwd){
     6         $sql = "select pwd from user where id='$uid'";
     7         $res = $this->dao->getStr($sql);//new对象调用返回字符串的方法
     8         if($pwd != "" && $res != "" && $pwd==$res){
     9             return true;
    10         }else{
    11             return false;
    12         }
    13     }
    14 }

    应用程序类后台文件夹模板(GoodsModel.class.php):

     1 <?php
     2 //商品表模型类
     3 class GoodsModel extends Model{
     4     //获取列表
     5     function getList(){
     6         $sql = "select * from p34_goods";
     7         return $this->dao->getAttr($sql);//new对象调用返回索引数组的方法
     8     }
     9     
    10     //添加列表
    11     function addData($data){
    12         $sql = "insert into p34_goods(goods_name,shop_price,goods_number) values ('".$data[goods_name]."','".$data[shop_price]."','".$data[goods_number]."')";//添加数据
    13         $this->dao->queryN($sql);//执行sql语句
    14     }
    15     
    16     //删除列表
    17     function delData($id){
    18         $sql = "delete from p34_goods where goods_id = '$id'";//删除数据
    19         $this->dao->queryN($sql);//执行sql语句
    20     }
    21 }

    应用程序类后台文件夹控制器(PlatFromController.class.php):

     1 <?php
     2 //平台控制器
     3 class PlatFromController extends Controller{
     4     //构造方法
     5     function __construct(){
     6         parent::__construct();
     7         $this->_checkLogin();
     8     }
     9     //验证登录操作
    10     function _checkLogin(){
    11         session_start();
    12         //不需要验证的方法名
    13         $no_list = array(
    14             "Login" => array("login","check","captcha")
    15         );
    16         //判断不需要验证的
    17         if(isset($no_list[CONTROLLER]) && in_array(ACTION,$no_list[CONTROLLER])){
    18             return;
    19         }
    20         //如果登录标志不存在
    21         if(!isset($_SESSION["login"])){
    22             $this->_jump("index.php?p=back&c=Login&a=login","亲,请先登录",1);
    23         }
    24     }
    25 }

    应用程序类后台文件夹控制器(LoginController.class.php):

     1 <?php
     2 //登陆首页的控制器
     3 class LoginController extends PlatFromController{
     4     //登录首页的方法
     5     function loginAction(){
     6         require VIEW_PATH."login.html";
     7     }
     8     //登录验证
     9     function checkAction(){
    10         $uid = $_POST["uid"];
    11         $pwd = $_POST["pwd"];
    12         $yzm = $_POST["captcha"];
    13         //验证码验证的判断
    14         $T_Captcha = new Captcha();
    15         if(!$T_Captcha->checkCaptcha($yzm)){
    16             $this->_jump("index.php?p=back&c=Login&a=login","验证码错误");//验证码错误
    17         }
    18         //连接数据库 把账号密码传过去  验证
    19         $userModel = Factory::M("userModel");
    20         //调用户表的模型类check  根据返回结果跳转
    21         if($userModel->check($uid,$pwd)){
    22             session_start();
    23             $_SESSION["login"] = "YES";//预防非法登录
    24             $this->_jump("index.php?p=back&c=Admin&a=index");//登录成功
    25         }else{
    26             $this->_jump("index.php?p=back&c=Login&a=login","登录失败");//登录失败
    27         }
    28     }
    29     
    30     //输出验证码的方法
    31     function captchaAction(){
    32         $yzm = new Captcha();
    33         $yzm->generate(6);
    34     }
    35     //退出登陆的方法
    36     function quitAction(){
    37         unset($_SESSION["login"]);
    38         $this->_jump("index.php?p=back&c=Login&a=login");//退出
    39     }
    40 }

    应用程序类后台文件夹控制器(GoodsController.class.php):

     1 <?php
     2 //商品控制器
     3 class GoodsController extends PlatFromController{
     4     //添加商品的方法
     5     function insertAction(){
     6         //获取数据
     7         $data['goods_name'] = $_POST['goods_name'];
     8         $data['shop_price'] = $_POST['shop_price'];
     9         $data['goods_number'] = $_POST['goods_number'];
    10         
    11         //上传商品图片原图
    12         $t_upload = new Upload();
    13         $t_upload->upload_path = './web/upload/';
    14         $t_upload->prefix = 'goods_ori_';
    15         if ($result = $t_upload->uploadOne($_FILES['goods_img'])) {
    16             //成功
    17             $data['goods_img'] = $result;
    18         } else {
    19             //上传失败
    20             $this->_jump('index.php?p=back&c=Admin&a=goodsAdd', '上传失败,<br>' . $t_upload->getError());
    21         }
    22         
    23         //获取商品的模型类对象
    24         $goodsModel = Factory::M("GoodsModel");
    25         
    26         //调用模型对象的添加方法
    27         $goodsModel->addData($data);
    28         
    29         //显示页面
    30         $this->_jump("index.php?p=back&c=Admin&a=goodsAdd","亲,添加成功");
    31         require VIEW_PATH."goodsAdd.html";
    32     }
    33 }

    应用程序类后台文件夹控制器(AdminController.class.php):

     1 <?php
     2 //后台首页控制器
     3 class AdminController extends PlatFromController{
     4     //验证是否具有登录标志
     5     function indexAction(){
     6         require VIEW_PATH."index.html";
     7     }
     8     //后台首页顶部的方法
     9     function topAction(){
    10         require VIEW_PATH."top.html";
    11     }
    12     //后台首页左侧的方法
    13     function menuAction(){
    14         require VIEW_PATH."menu.html";
    15     }
    16     //后台首页右侧的方法
    17     function mainAction(){
    18         require VIEW_PATH."main.html";
    19     }
    20     //后台首页左侧商品列表的方法
    21     function goodsListAction(){
    22         //实例化模型类
    23         $goodsModel = Factory::M("GoodsModel");
    24         //返回数据
    25         $list = $goodsModel->getList();
    26         require VIEW_PATH."goodsList.html";
    27     }
    28     //后台首页左侧添加新商品的方法
    29     function goodsAddAction(){
    30         require VIEW_PATH."goodsAdd.html";
    31     }
    32     //删除商品
    33     function delAction(){
    34         //接收前端页面传过来的id
    35         $id = $_GET["id"];
    36         //实例化商品模型类
    37         $goodsModel = Factory::M("GoodsModel");
    38         //调用删除方法
    39         $goodsModel->delData($id);
    40         //显示页面
    41         $list = $goodsModel->getList();
    42         require VIEW_PATH."goodsList.html";
    43     }
    44 }
  • 相关阅读:
    Scrapy学习-18-去重原理
    Scrapy学习-17-暂停和重启
    Scrapy学习-16-动态网页技术
    Scrapy学习-15-降低被识别为爬虫的方法
    Scrapy学习-14-验证码识别
    Scrapy学习-13-使用DownloaderMiddleware设置IP代理池及IP变换
    Scrapy学习-12-使用DownloaderMiddleware随机修改User-Agent
    Scrapy学习-11-Selector对象使用
    使用grunt完成requirejs的合并压缩和js文件的版本控制
    nodemailer中的几个坑
  • 原文地址:https://www.cnblogs.com/Prinlily/p/9810735.html
Copyright © 2011-2022 走看看