标签:space return unset ext turn blog catch 控制器 加载类
目录结构:
core\lib\conf.php
<?php
namespace core\lib;
class conf
{
static public $conf = [];
static function get($name,$file) {
/*
* 1.判断配置文件是否存在
* 2.判断配置项是否存在
* 3.缓存配置
*/
if(isset(self::$conf[$file])) {
return self::$conf[$file][$name];
} else {
$path = MYFRAME.‘/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 function all($file)
{
if(isset(self::$conf[$file])) {
return self::$conf[$file];
} else {
$path = MYFRAME.‘/core/config/‘.$file.‘.php‘;
if(is_file($path)) {
$conf = include $path;
self::$conf[$file] = $conf;
return $conf;
} else {
throw new \Exception(‘配置文件不存在‘.$file);
}
}
}
}
数据库配置文件 core\config\database.php
<?php
return array(
‘DSN‘ => ‘mysql:host=localhost;dbname=test‘,
‘USERNAME‘ => ‘root‘,
‘PASSWD‘ => ‘root‘,
);
默认路由配置文件 core\config\route.php
<?php
return array(
‘CTRL‘=>‘Index‘,
‘ACTION‘=>‘index‘
);
加载数据库配置文件 \core\lib\model.php
<?php
namespace core\lib;
use \core\lib\conf;
class model extends \PDO
{
public function __construct()
{
$dsn = conf::get("DSN",‘database‘);
$username = conf::get("USERNAME",‘database‘);
$passwd = conf::get("PASSWD",‘database‘);
try {
parent::__construct($dsn,$username,$passwd);
} catch (\Exception $e) {
p($e->getMessage());
}
}
}
加载路由配置文件 core\lib\Route.php
<?php
namespace core\lib;
use core\lib\conf;
class Route
{
public $controller;//控制器
public $action;//方法
function __construct()
{
/*
* 1.隐藏index.php
* 2.返回url参数部分
* 3.返回控制器和方法
*/
if (isset($_SERVER[‘REDIRECT_URL‘]) && $_SERVER[‘REDIRECT_URL‘] != ‘/‘) {
$path = $_SERVER[‘REDIRECT_URL‘];
$pathArr = explode(‘/‘,trim($path,‘/‘));
if (isset($pathArr[0])) {
$this->controller = $pathArr[0];
}
unset($pathArr[0]);
if (isset($pathArr[1])) {
$this->action = $pathArr[1];
unset($pathArr[1]);
}else{
//从配置文件加载默认方法
$this->action = conf::get(‘CTRL‘,‘route‘);
}
//url多余部分(参数)转为GET
$count = count($pathArr)+2;
$i = 2;
while ($i < $count) {
if (isset($pathArr[$i+1])) {
$_GET[$pathArr[$i]] = $pathArr[$i+1];
}
$i += 2;
}
} else {
$this->controller = conf::get(‘CTRL‘,‘route‘);;//默认控制器
$this->action = conf::get(‘ACTION‘,‘route‘);;//默认方法
}
}
}
标签:space return unset ext turn blog catch 控制器 加载类
原文地址:https://www.cnblogs.com/xiaobingch/p/12464613.html