一)项目的入口文件
1.新建项目文件夹:my_frame,在文件夹里新建文件index.php,
2.新建my_frame/core核心文件夹,my_frame/app项目所储文件夹
3.新建core/common/function.php函数库,core/ lss.php核心文件
4.自动加载文件(启动框架)
入口文件index.php具体实现代码如下:
/**
* 入口文件
* 1.定义全局常量(define)
* 2.加载函数库
* 3.启动框架
*/
define('LSS',dirname(__FILE__));#当前框架所在的根目录
define('CORE',LSS.'/core');#框架核心文件所在目录
define('APP',LSS.'/app');#项目文件所处目录
define('MODULE','app');#项目文件所处目录
define('DEBUG',true);#是否开始调试模式
if (DEBUG){
ini_set('display_error','On');#为一个配置选项设置值
}else{
ini_set('display_error','Off');
}
include CORE.'/common/function.php';#加载函数库
include CORE.'/lss.php';#核心文件
spl_autoload_register('\core\lss::load');#类不存在的时候将会触发该方法,即类的自动加载
\core\lss::run();
二)类自动加载
自动加载事先定义好的类
my_frame/core/lss.php
namespace core;#命名空间
class lss{
/**
* 基础类,自动加载类库
* @param $class
* @return bool
*/
static public function load($class){
//new \core\route();
//$class = '\core\route';
//LSS.'/core/route.php';
var_dump($class);
if (isset($classMap[$class])){#类存在,即不引用类
return true;
}else{
$class = str_replace('\\','/',$class);
$file = LSS.'/'.$class.'.php';
if (is_file($file)){
include $file;#引用文件
self::$classMap[$class] = $class;
}else{
return false;
}
}
}
}
三)配置文件类
my_frame/core/lib/conf.php
为了更好修改配置内容……
<?php
namespace core\lib;
class conf{
static public $conf=[];#存放配置文件
/**
* 加载单个配置文件
* 经常要用到,所以定义为静态的
* @param $name
* @param $file
* @return mixed
* @throws \Exception
*/
static public function get($name,$file){
/**
* 1.判断配置文件是否存在
* 2.判断配置是否存在
* 3.缓存配置
*/
if (isset(self::$conf[$file])){
return self::$conf[$file][$name];
}else {
$path = LSS . '/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);
}
}
}
/**
* 引入全部文件
* @param $file
* @return mixed
* @throws \Exception
*/
static public function all($file){
if (isset(self::$conf[$file])){
return self::$conf[$file];
}else {
$path = LSS . '/core/config/' . $file . '.php';
if (is_file($path)) {
$conf = include $path;
self::$conf[$file] = $conf;
return $conf;
} else {
throw new \Exception('找不到配置文件' . $file);
}
}
}
}
四)路由类
my_frame/core/lib/route.php
<?php
namespace core\lib;
use core\lib\conf;
class route{
public $ctrl;#控制器
public $action;#方法
public function __construct()
{
//xx.com/index.php/index/index
//xx.com/index/index
/**
* 1.隐藏index.php文件,引入.htaccess文件,与入口文件同级
* 2.获取URL 参数部分,即get传值
* 3.返回对应控制器和方法
*/
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');
}
//url 多余部分转换成GET eg:index/index/id/1 实现get传值
$count = count($pathArr) + 2;
$i = 2;
while ($i < $count){
if (isset($pathArr[$i+1])){
$_GET[$pathArr[$i]] = $pathArr[$i+1];
}
$i = $i + 2;
};
}else{
$this->ctrl = conf::get('CTRL','route');
$this->action = conf::get('ACTION','route');
}
}
}
my_frame/.htaccess
<IfModule mod_rewrite.c>
# Options +FollowSymlinks -Multiviews
RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>
my_frame/core/config/route.php
<?php
return array(
'CTRL' =>'index',
'ACTION'=>'index',
);
五)控制器方法
my_frame/core/lss.php
<?php
namespace core;
class lss{
public static $classMap = array();#用于判断类是否存在,节约性能
/**
* 运行控制器和方法
* @throws \Exception
*/
static public function run(){
$route =new \core\lib\route();
$ctrlClass = $route->ctrl;
$action = $route->action;
$ctrlFile = APP.'/controller/'.$ctrlClass.'.php';#控制器文件
$ctrlClass = '\\'.MODULE.'\controller\\'.$ctrlClass;
if (is_file($ctrlFile)){
include $ctrlFile;
$ctrl = new $ctrlClass();
$ctrl->$action();
}else{
throw new \Exception('找不到控制器'.$ctrlClass);
}
}
}
my_frame/app/controller/index.php
<?php
namespace app\controller;
class index extends \core\lss {
public function index(){
$temp = \core\lib\conf::get('CTRL','route');
$temp1 = \core\lib\conf::get('ACTION','route');
print_r($temp);
print_r($temp1);
}
}
六)模型类
my_frame/core/lib/model.php
<?php
namespace core\lib;#命名空间
class model extends \PDO{
public function __construct()
{
$database = conf::all('database'); //引入全部文件
try{
parent::__construct($database['DSN'], $database['USERNAME'], $database['PASSWD']);
}catch (\PDOException $e){
print_r($e->getMessage());
}
}
}
my_frame/core/config/datebase.php
<?php
return array(
'DSN' => 'mysql:host=localhost;dbname=tieba',
'USERNAME' => 'root',
'PASSWD' => 'root',
);
七)视图类
my_frame/core/lss.php
<?php
namespace core;
class lss{
public $assign;#用于判断类是否存在,节约性能
/**
* 变量名替换
* @param $name
* @param $value
*/
public function assign($name,$value){
$this->assign[$name] = $value;
}
/**
* 显示视图
* @param $file
*/
public function display($file){
$file = APP.'/views/'.$file;
if (is_file($file)){
extract($this->assign);#从数组中将变量导入到当前的符号表
include $file;
}
}
}
my_frame/app/views/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<?php echo $data.' '.$title; ?>
</body>
</html>
my_frame/app/controller/index.php
<?php
namespace app\controller;
class index extends \core\lss {
public function index(){
#视图功能
$data = '你好,lss';
$title = '视图文件';
$this->assign('title',$title);
$this->assign('data',$data);
$this->display('index.html');
}
}