学习封装 MVC (3)

来源:互联网 发布:android 调用js 编辑:程序博客网 时间:2024/05/21 13:21

一、配置加载类

         

        1)创建配置路由


return array(    'CTRL'=>'index',    'ACTION'=>'index');

       2)控制器层

         1.判断配置文件是否存在

         2.判断当前配置是否存在

         3.缓存配置

          (加载单一配置)

    

    static public $conf=array();    static public function get($name,$file){           $file= BAO.'\core\config\\'.$file.'.php'; //拼接配置文件地址        //判断配置文件            if(isset(self::$conf[$file])){            return self::$conf[$file][$name];        }else{                       if(is_file($file)){                $conf=include $file; //存在包含这个配置                //判断当前配置                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){        $file= BAO.'\core\config\\'.$file.'.php';  //拼接配置文件地址               //判断配置文件            if(isset(self::$conf[$file])){            return self::$conf[$file]; //返回配置文件        }else{          //判断当前配置            if(is_file($file)){                $conf=include $file;                self::$conf[$file]=$conf;                return  $conf;//返回当前整个配置            }else{                throw new \Exception('找不到配置文件'.$file);            }        }    }

 二、配置日志类

      1.确定日志的储存方式

       2.写日志

     1)创建日志的配置

      

return array(    'DRIVE'=>'file', //驱动文件     //驱动路径   'OPTION'=>array(        'PATH'=>BAO.'/log/'    ));

     2)创建日志的驱动

    static $class;    static public function init(){        //确定储存方式        $drive=conf::get('DRIVE','log');        $class= '\core\lib\drive\log\\'.$drive;//拼接路径         self::$class=new $class;    }
   

      3)创建日志存储的路径

      

    public $path;//日志存储位置    //使用构造方法    public function __construct()    {        $conf = conf::get('OPTION', 'log');        $this->path = $conf['PATH'];    }

      4)写日志

        1.确定文件的存储位置是否存在

        2.如果不存在则新建

        3.写入日志

 public function log($message, $file = 'log')    {       //判断目录是否存在,没有则按每小时新建一个目录        if (!is_dir($this->path.date('YmdH'))) {           mkdir($this->path.date('YmdH'), '0777', true);        }        $day = date('Y-m-d H:i:s');        //使用 Json 格式写入时间目录       $cd=file_put_contents($this->path.date('YmdH') .'/'.$file.'.php',$day.json_encode($message).PHP_EOL,FILE_APPEND);        return $cd;    }

      5)修改日志目录为最高权限!

       





    

    

    





     

        

      

      

0 0