MVC配置加载类

来源:互联网 发布:python for 无限循环 编辑:程序博客网 时间:2024/05/18 22:14

之前我们的路由所有的配置都是写在当前文件中的接下来我们开始写一个配置类通过配置类去加载配置
在lib文件夹下创建conf.php

<?phpnamespace core\lib;class conf{static public $conf=array();    //$name 加载的配置名称    //$file 加载的文件    static public function get ($name,$file)    {    /*    *1.判断配置文件是否存在    *2.判断对应的配置是否存在    *3.缓存配置    */    if(isset(self::$conf[$file]))        {            return self::$conf[$file][$name];        }        else        {            $path = WTY.'\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);            }        }

在core目录下创建config文件夹用来存放配置文件
先建立一个路由的配置文件
route.php

<?phpreturn array(    'CTRL'=>'index',//默认控制器    'ATION'=>'index'//默认方法名)

接下来修改路由类通过配置类来加载默认项

<?php namespace core\lib; use core\lib\conf; //加入配置类  /* *路由  */ class route {    public $ctrl;    public $action;    public function __construct()    {        //p($_SERVER);        if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']!='/')        {            $path=$_SERVER['REQUEST_URI'];            $patharr=explode('/',trim($path,'/'));            if(isset($patharr[0]))            {                $this->ctrl=$patharr[0];                unset($patharr[0]);            }            if(isset($patharr[1]))            {                $this->action =$patharr[1];                unset($patharr[1]);            }            else            {                $this->action =conf::get('ACTION','route');//通过配置项加载方法名            }            //p($patharr);            //url 多余的部分转换成 GET 参数            $count= count($patharr)+2;            $i=2;            while($i<$count)            {                if(isset($patharr[$i+1]))                {                $_GET[$patharr[$i]]=$patharr[$i+1];                                   }                $i+=2;            }            unset($_GET['url']);            //p($_GET);        }        else        {            $this->ctrl   = conf::get('CTRL','route'); //通过配置项加载控制器名            $this->action = conf::get('ACTION','route');        }    }    }

同样我们在config下创建database.php数据库配置文件

    static public function all($file)    {        if(isset(self::$conf[$file]))        {            return self::$conf[$file];        }        else        {            $path = WTY.'\core\config\\'.$file.'.php';            if(is_file($path))            {                $conf = include $path;                self::$conf[$file] = $conf;                return $conf;            }            else            {                throw new \Exception('没有配置文件'.$file);            }        }    }

接下来修改model类

<?phpnamespace core\lib;use core\lib\conf;class model extends \medoo{    public function __construct()    {        $data=conf::all('database');    try{    parent::__construct($data['DSN'],$data['USERNAME'],$data['PASSWD'])eatch(\PDOException $e)        {        p($e->getMessage());        }    }    }}

去控制器中测试

$model = new \app\model\mvcModel();p($model);
0 0
原创粉丝点击