17)将index.php中的代码放到Framework中封装起来

目录结构:

    

发生改动的类代码:

    新增类:Framework.class.php

  1 <?php
  2     /**
  3      * Created by PhpStorm.
  4      * User: Interact
  5      * Date: 2017/8/21
  6      * Time: 16:01
  7      */
  8 class Framework{
  9     /**
 10      * 入口
 11      */
 12     public static function run() {
 13         //声明路径常量
 14         static::_initPathConst();
 15         //确定分发参数
 16         static::_initDispatchParam();
 17         // 当前平台相关的路径常量
 18         static::_initPlatformPathConst();
 19         // 注册自动加载
 20         self::_initAutoload();
 21         // 请求分发
 22         static::_dispatch();
 23     }
 24     /**
 25      * 声明路径常量
 26      */
 27     private static function _initPathConst() {
 28         //目录基础常量的定义
 29         //目录地址常量
 30         define('ROOT_PATH',getcwd().'/');
 31         define('APPLICATION_PATH',ROOT_PATH.'application'.'/');
 32         define('FRAMEWORK_PATH',ROOT_PATH.'framework'.'/');
 33         define('TEST_PATH',APPLICATION_PATH.'test'.'/');
 34         define('CONTROLLER_PATH',TEST_PATH.'controller'.'/');
 35         define('MODEL_PATH',TEST_PATH.'model'.'/');
 36         define('VIEW_PATH',TEST_PATH.'view'.'/');
 37         define('TOOL_PATH',FRAMEWORK_PATH.'tool'.'/');
 38     }
 39     /**
 40      * 初始化分发参数
 41      */
 42     private static function _initDispatchParam() {
 43         // 确定分发参数
 44         // 平台
 45         //确定分发参数
 46         //动作
 47         define('CONTROLLER',isset($_GET['c'])?$_GET['c']:'zixunC');
 48         define('ACTION',isset($_GET['a'])?$_GET['a']:'show');
 49         define("PLATFORM",isset($_GET['p'])?$_GET['p']:'test');
 50     }
 51     /**
 52      * 声明当前平台路径常量
 53      */
 54     private static function _initPlatformPathConst() {
 55         //当前平台相关的路径常量
 56         define('CURRENT_CONTROLLER_PATH', APPLICATION_PATH . PLATFORM . '/controller/');
 57         define('CURRENT_MODEL_PATH', APPLICATION_PATH . PLATFORM . '/model/');
 58         define('CURRENT_VIEW_PATH', APPLICATION_PATH . PLATFORM . '/view/');
 59     }
 60     /**
 61      * 自动加载方法
 62      */
 63    public static function userautoload($class_name){
 64         
 65         //        var_dump($class_name);
 66         //先处理确定的(框架中的核心类)
 67         // 类名与类文件映射数组
 68         $framework_class_list = array(
 69             // '类名' => '类文件地址'
 70             'Controller' => FRAMEWORK_PATH.'Controller.php',
 71             'Model' => FRAMEWORK_PATH.'Model.class.php',
 72             'Factory' => FRAMEWORK_PATH.'Factory.class.php',
 73             'MySQLDB' => FRAMEWORK_PATH.'MySQLDB.class.php',
 74             'SessionDB'=>TOOL_PATH.'SessionDB.class.php',
 75         ) ;
 76         //        echo "走没走";
 77         //判断是否为核心类
 78         if (isset($framework_class_list[$class_name])) {
 79             //是核心类
 80             require $framework_class_list[$class_name];
 81         }
 82         //判断是否为可增加(控制器类,模型类)
 83         //控制器类,截取后是个字符,匹配Controller
 84         elseif (substr($class_name, -1) == 'C') {
 85             // 控制器类, 当前平台下controller目录
 86             require APPLICATION_PATH . PLATFORM . '/controller/' . $class_name . '.controller.class.php';
 87         }
 88         //模型类,截取后5个字符,匹配Model
 89         elseif (substr($class_name, -5) == 'Model') {
 90             // 模型类,当前平台下model目录
 91             require APPLICATION_PATH . PLATFORM . '/model/' . $class_name . '.class.php';
 92         }
 93         
 94     }
 95     /**
 96      * 注册自动加载
 97      */
 98     private static function _initAutoload() {
 99         spl_autoload_register(array(__CLASS__, 'userAutoload'));
100     }
101     /**
102      * 分发请求
103      */
104     private static function _dispatch() {
105         require APPLICATION_PATH.PLATFORM.'/controller/'.CONTROLLER.'.controller.class.php';
106         $controlelr_name=CONTROLLER;
107         $controller=new $controlelr_name();
108         $action_name=ACTION;
109         $controller->$action_name();
110     }
111 }

  index.php代码展示:

  

1  require './framework/Framework.class.php';
2     Framework::run();

结果展示:

  

原文地址:https://www.cnblogs.com/xiaoyoucai/p/7405695.html