PHP学习笔记,自己动手写个MVC的框架 -- base所有代码

<?php
/**
名称:基础类
作用:引用参数处理类,加载基础配置,
作者:swordphp@126.com
创建日期:2013-07-31
**/
require_once(ROOT_PATH.'/system/lib/configs/sys_path.config.php');
class base{
    public static $sys;
    private function __construct(){
        return false;
    }
    public static function getInstance(){
        if(!(self::$sys instanceof self)){
            self::$sys = new self();
        }
        return self::$sys;
    }
    //初始化方法
    public static function init(){
        self::sys_class('model');
        self::sys_class('ctrl');
        $args = self::__explan_arg();
        $ctrl = self::__load_ctrl($args['m'],$args['c']);
        call_user_func(array($ctrl,$args['a']));
    }
    //加载系统类
    public static function sys_class($classname){
        $file = SYS_CLASS_DIR.$classname.'.class.php';
        if(is_file($file)){
            include_once($file);
        }else{
            exit('class file not found!FILE'.$file);
        }
    }
    //加载数据模型
    public function load_model($modelname){
        $file = MODEL_DIR.$modelname.".model.class.php";
        if(is_file($file)){
            include_once $file;
            $classname = $modelname.'_model';
            return $model = new $classname;
        }else{
            exit('model file not found!');
        }
    }
    private function __clone(){
        //防止单例类被复制
    }
    //加载系统函数
    public static function sys_fun(){

    }
    //加载配置文件
    public static function load_config($dir,$name,$key='default'){
        $configs = array();
        $file = $dir.$name.'.config.php';
        if(is_file($file)){
            $configs = include_once($file);
            if(isset($configs[$key])){
                return $configs[$key];
            }else{
                exit("config wrong,not found!");
            }
        }else{
            //exit("config file not exist!FILE:".$file);
        }
    }
    //加载系统的配置文件
    public static function sys_config($name,$key='default'){
        $file = SYS_CONFIG_DIR;
        return self::load_config($file,$name,$key);
    }

    private static function __load_class($dir,$name){

    }
    //加载控制器
    //$m是模块名称
    //$c是控制器名称
    private static function __load_ctrl($m,$c){
        $c_dir = MODULES_DIR.$m."/";
        if(is_dir($c_dir)){
            include($c_dir.$c.'.php');
            $classname = 'c_'.$c;
            return new $classname;
        }else{
            exit("modules dir not exist!");
        }
    }
    //解析URL中的参数
    private static function __explan_arg(){
        $default_arg = self::sys_config('default_arg');
        $args['m'] = isset($_GET['m'])?$_GET['m']:$default_arg['m'];
        $args['c'] = isset($_GET['c'])?$_GET['c']:$default_arg['c'];
        $args['a'] = isset($_GET['a'])?$_GET['a']:$default_arg['a'];
        return $args;
    }
}

 

posted @ 2013-08-20 10:33  第一缕阳光  阅读(373)  评论(0编辑  收藏  举报