mvc配置加载类

来源:互联网 发布:gta4优化补丁告别卡顿 编辑:程序博客网 时间:2024/05/18 21:49

一、配置加载类

1)首先我们要新建一个配置类,由于配置类经常会被用到,所以我们将他写成静态方法,我们需要俩个参数一个是我们就要加载的配置的名称,第二个就是我们要加载的文件,然后我们将它对应的文件放在config文件当中,然后在这个配置文件当中去定义我们在默认情况下的路由,在定义方法名与控制器名。
这里写图片描述

class conf {     static $conf = array();//将配置文件放入$conf    static public function get($name,$file){        $file = MVC.'\core\config\\'.$file.'.php';            // var_dump(self::$conf);        if (isset(self::$conf[$file])) {            return self::$conf[$file][$name];            } else {            // var_dump('1');            if (is_file($file)) {                $conf = include $file;                if (isset($conf[$name])) {                    self::$conf[$file] = $conf;                    return $conf[$name];                 } else {                    throw new \Exception("没有这个配置文件", 1);                                        }            } else {                throw new \Exception("找不到配置文件", 1);            }        }    }    //直接引入一个配置文件    static public function all($file){        if(isset(self::$conf[$file])){            return self::$conf[$file];        } else {            $path = MVC.'\core\config\\'.$file.'.php';            // var_dump($path);            if (is_file($path)) {                //如果存在 将配置放入config                $conf = include $path;                self::$conf[$file] = $conf;                return $conf;            } else {                throw new \Exception("没有这个配置文件", 1);                                }            // self::$conf[$file] = $conf[$file];        }    }}

在数据库与路由类中我们说到过 是通过这个$CONF中读取的文件,数据库是直接读取的文件,而路由中是读取的某个值。
当我们需要引用这个conf类的时候 需要在命名空间下引用:

use \core\lib\conf;
0 0
原创粉丝点击