PHP搭建自己的MVC框架9 配置加载类

来源:互联网 发布:游族网络 刀剑乱舞 编辑:程序博客网 时间:2024/05/16 11:46

框架中在core 目录下新建config目录 分别是database 和 route  如下图所示:

同时在lib 建conf 类文件

代码如下:

<?phpnamespace core\lib;class conf{    static public $conf = array();     static public function get($name,$file){        /*         * 判断配置文件是否存在         * 判断配置是否存在         * 缓存配置         */        if(isset(self::$conf[$file])){            return self::$conf[$file][$name];        }else{            $path= MYMVC .'/core/config/'.$file.'.php';            if(is_file($path)){                $conf = include $path;                if(isset($conf[$name])){                    self::$conf[$file] = $conf;                    return $conf[$name];                }else{                    throw new \Exception('找不到配置项'.$name);                }            }else{                throw new \Exception('找不到配置文件'.$file);            }        }     }     static public function all($file){         if(isset(self::$conf[$file])){             return self::$conf[$file];         }else{             $path= MYMVC .'/core/config/'.$file.'.php';             if(is_file($path)){                 $conf = include $path;                 self::$conf[$file] = $conf;                 return $conf;             }else{                 throw new \Exception('找不到配置文件'.$file);             }         }     }}
最后默认的控制器和方法 都是index

数据库的配置也如下 所示:

<?phpreturn array(    'CTRL'=>'index',    'ACTION'=>'index',);

<?phpreturn array(    'DSN'=>'mysql:host=localhost;dbname=test',    'USERNAME'=>'root',    'PASSWD'=>'root',);


0 0
原创粉丝点击