学习封装 MVC (3)

一、配置加载类

         

        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)修改日志目录为最高权限!

       





    

    

    





     

        

      

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值