├── app ├── view │ └── Index │ │ ├── page │ │ ├── register.php │ │ └── index.php │ │ ├── footer.php │ │ └── header.php ├── model │ └── userModel.php └── controller │ └── IndexController.php ├── miniphp ├── utils │ └── Safe.php ├── config │ └── Config.php ├── Controller.php ├── Model.php ├── Route.php └── core │ └── Psr4AutoLoad.php ├── index.php └── README.md /app/view/Index/page/register.php: -------------------------------------------------------------------------------- 1 | 我是注册页面 2 |
-------------------------------------------------------------------------------- /app/view/Index/footer.php: -------------------------------------------------------------------------------- 1 | 2 |


3 | 6 | 7 | -------------------------------------------------------------------------------- /app/view/Index/page/index.php: -------------------------------------------------------------------------------- 1 | INDEX页面 2 |
3 | 调用页面参数: 4 |
5 | parameter);?> 6 |
7 | 加密后的字符串: 8 |
9 | parameter['passwd'];?> 10 |
-------------------------------------------------------------------------------- /miniphp/utils/Safe.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Mini_PHP_Frame Demo 4 | 5 | 6 |
7 |

欢迎使用 Mini_PHP_Frame

8 |
9 |
10 |
11 |
12 |
-------------------------------------------------------------------------------- /miniphp/config/Config.php: -------------------------------------------------------------------------------- 1 | "yourCryptSalt", 6 | 7 | //数据库配置 8 | 'db' => array( 9 | //地址 10 | 'ip' => 'localhost', 11 | //用户名 12 | 'user' => 'root', 13 | //用户密码 14 | 'password' => 'password', 15 | //连接的库名 16 | 'name' => 'db_name' 17 | ) 18 | ); 19 | 20 | //自定义路由 21 | const __ROUTE__ = array( 22 | '/register/' => "/Index/register", 23 | '/loginxxx/' => "/Index/login/xxx", 24 | '/archive/2/' => "/Archive/list/2", 25 | ); -------------------------------------------------------------------------------- /miniphp/Controller.php: -------------------------------------------------------------------------------- 1 | page_view, $dir); 21 | } 22 | 23 | public function __destruct() 24 | { 25 | //渲染视图 26 | if($this->page_view != null) 27 | { 28 | foreach($this->page_view as $p) 29 | { 30 | include_once($p); 31 | } 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /app/model/userModel.php: -------------------------------------------------------------------------------- 1 | table_name = substr(__CLASS__, 0, -5); 12 | } 13 | 14 | //示例 15 | public function getUserInfo($userName) 16 | { 17 | $stmt = $this->sql_connect->prepare("select * from ? where user = '?';"); 18 | $result = $stmt->bind_param("ss", $this->table_name, $userName)->excute()->fetch_array(); 19 | $stmt->close(); 20 | 21 | return $result; 22 | } 23 | 24 | public function __destruct() 25 | { 26 | parent::__destruct(); 27 | } 28 | } -------------------------------------------------------------------------------- /miniphp/Model.php: -------------------------------------------------------------------------------- 1 | table_bname = $conf['name']; 16 | 17 | $sql = mysqli_connect($conf['ip'], $conf['user'], $conf['password'], $conf['name']); 18 | 19 | if(mysqli_connect_error()) 20 | { 21 | die('MySql连接失败'); 22 | } 23 | 24 | //设置数据库编码为UTF8以支持中文 25 | $sql->query("set character set 'utf8';"); 26 | $sql->query("set names 'utf8';"); 27 | 28 | $this->sql_connect = $sql; 29 | } 30 | 31 | public function __destruct() 32 | { 33 | mysqli_close($this->sql_connect); 34 | } 35 | } -------------------------------------------------------------------------------- /app/controller/IndexController.php: -------------------------------------------------------------------------------- 1 | page_include("Index/header.php"); 12 | } 13 | 14 | public function index() 15 | { 16 | $this->parameter['page_name'] = '首页'; 17 | 18 | $Safe = new \miniphp\utils\Safe(); 19 | $this->parameter['passwd'] = $Safe->cryptStr('你需要加密的字符串'); 20 | 21 | $this->page_include("Index/page/index.php"); 22 | } 23 | 24 | public function register() 25 | { 26 | $this->parameter['page_name'] = '注册'; 27 | 28 | $this->page_include("Index/page/register.php"); 29 | } 30 | 31 | private function getUserModelConnect() 32 | { 33 | //此函数用于获取User表的数据库类实例 34 | //无需 include 直接 new(自动加载会帮你include) 35 | return new userModel(); 36 | } 37 | 38 | public function __destruct() 39 | { 40 | $this->page_include("Index/footer.php"); 41 | 42 | parent::__destruct(); 43 | } 44 | } -------------------------------------------------------------------------------- /miniphp/Route.php: -------------------------------------------------------------------------------- 1 | routeConfig = $this->getRouteConfig(); 12 | } 13 | 14 | public function run() 15 | { 16 | if($this->routeConfig || is_array($this->routeConfig)) 17 | { 18 | $url = explode('/', __URL__); 19 | //把 xxx.com/c/a/ 转换为 /c/a 20 | if($url[1] != NULL) 21 | { 22 | $ru = NULL; 23 | foreach($url as $key => $u) 24 | { 25 | if($key == 0) 26 | { 27 | $ru = '/'; 28 | } 29 | else 30 | { 31 | if($u != NULL) 32 | { 33 | $ru = $ru . $u . '/'; 34 | } 35 | } 36 | } 37 | //判断访问的URL是否存在路由配置中 38 | foreach($this->routeConfig as $left => $right) 39 | { 40 | if($ru === $left) 41 | { 42 | $url = $url[0] . $right; 43 | return $url; 44 | } 45 | } 46 | return __URL__; 47 | } 48 | else 49 | { 50 | return __URL__; 51 | } 52 | } 53 | else 54 | { 55 | return __URL__; 56 | } 57 | } 58 | 59 | public function getRouteConfig() 60 | { 61 | return __ROUTE__; 62 | } 63 | } -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | run(); 29 | //处理URL 30 | $url = explode('/', $url); 31 | 32 | //c(Controller)转换为全小写 首字母大写,默认值为Index,a(Action)方法名 33 | $c = @$url[1] ? ucfirst(strtolower($url[1])) : 'Index'; 34 | $a = @$url[2] ? $url[2] : 'index'; 35 | 36 | //拼接带有命名空间的类名 37 | $controller = '\\controller\\' . $c . 'Controller'; 38 | 39 | //添加命名空间映射 40 | $psr->addMap('controller', 'app/controller'); 41 | $psr->addMap('model', 'app/model'); 42 | 43 | $class = new $controller(); 44 | 45 | //判断方法是否存在 46 | if(method_exists($class, $a)) 47 | { 48 | //调用目标方法 49 | call_user_func([$class, $a]); 50 | } 51 | else 52 | { 53 | die($c . '/' . $a . ' 方法不存在'); 54 | } -------------------------------------------------------------------------------- /miniphp/core/Psr4AutoLoad.php: -------------------------------------------------------------------------------- 1 | mapLoad($nameSpace, $realClass); 34 | } 35 | } 36 | 37 | public function mapLoad($nameSpace, $realClass) 38 | { 39 | //判断是否映射过 40 | if(array_key_exists($nameSpace, $this->maps)) 41 | { 42 | $nameSpace = $this->maps[$nameSpace]; 43 | } 44 | 45 | //处理路径 46 | $nameSpace = rtrim(str_replace('\\/' , '/', $nameSpace), '/') . '/'; 47 | //拼接文件全路径 48 | $filePath = $nameSpace . $realClass . '.php'; 49 | 50 | if(file_exists($filePath) && !class_exists($realClass)) 51 | { 52 | include_once($filePath); 53 | } 54 | else 55 | { 56 | die($realClass . ' 类不存在'); 57 | } 58 | } 59 | 60 | //命名空间 路径 将命名空间和路径保存到映射数组中 61 | public function addMap($nameSpace, $path) 62 | { 63 | if(array_key_exists($nameSpace, $this->maps)) 64 | { 65 | die($nameSpace . ' 该命名空间已经映射'); 66 | } 67 | 68 | //将命名空间和路径以键值对形式存放到数组中 69 | $this->maps[$nameSpace] = $path; 70 | } 71 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mini_PHP_Frame MVC框架 2 | 环境要求: PHP7+, Linux, Apache/Nginx 3 |
4 |

超级轻的PHP Web框架

5 | 代码仅10 KB! 6 |
7 | 演示地址: [点我] 8 | 9 |

文件结构

10 |
11 | app                      |项目文件
12 |   controller             |控制器
13 |     IndexController.php  |默认控制器
14 |   model                  |数据库模型
15 |     userModel.php        |默认模型
16 |   view                   |页面文件
17 |     page                 |page页面
18 |       index.php          |index页面内容
19 |     header.php           |index头部内容
20 |     footer.php           |index底部内容
21 | miniphp                  |框架文件
22 |   config                 |设置
23 |     Config.php           |设置文件
24 |   core                   |核心
25 |     Psr4AutoLoad.php     |自动加载(Psr4)
26 |   utils                  |工具
27 |     Safe.php             |安全(加密,过滤)
28 |   Controller.php         |控制器(父类)
29 |   Model.php              |模型(父类)
30 |   Route.php              |自定义路由
31 | index.php                |框架入口
32 | 
33 | 34 |

初次使用

35 | 下载项目文件,放在网页根目录 36 |
37 | 配置重定向(所有不存在的访问重定向到 /index.php) 38 |
39 | Nginx配置: 40 |
41 | location /
42 | {
43 |     try_files $uri $uri/ /index.php?$query_string;
44 | }
45 | 
46 | Apache请自行设置 47 |
48 | 访问以下任意地址 49 |
50 | http://localhost 51 |
52 | http://localhost/Index/index 53 |
54 | http://localhost/register 55 | 56 |

自定义路由(Route)

57 | 路由设置在miniphp/config/Config.php 58 |
59 | 访问 xxx.com/register 60 |
61 | 相当于访问 62 |
63 | xxx.com/Index/register 64 |
65 | 66 |

命名规范

67 | MySQL的库/表名需小写或小写加下划线,如:item,car_orders 68 |
69 | 控制器(Controller)需用大驼峰命名法,即首字母大写,并在名称后添加Controller,如:IndexController 70 |
71 | 方法名(Action)需用小驼峰命名法,即首字母小写,如:index,indexPost 72 |
73 | 模型名(Model)需用小骆驼峰命名法,首字母小写,userModel(父类用大骆驼峰命名法) 74 |
75 | 76 |

已定义常量

77 |
78 | __URL__      (string)   用户访问的URL
79 | __ROUTE__    (array)    自定义路由配置
80 | __CONFIG__   (array)    全局配置
81 | __APP_PATH__ (string)   App路径
82 | 
83 | 84 |

其他

85 | 默认控制器: Index 默认方法: index 86 |
87 | 所以: 访问 xxx.com 等同于访问 xxx.com/Index/index 88 |
89 | 可在入口文件/index.php 中修改默认控制器和默认方法 90 |
91 | URL结构: xxx.com/控制器/方法/参数1/参数2/参数3/参数+ 92 |
93 | 获取URL参数: $url[3]为参数1 $url[4]为参数2 94 |
95 |
96 | 最后更新时间:2021/3/24 97 | --------------------------------------------------------------------------------