Blogger Information
Blog 65
fans 2
comment 0
visits 60384
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自撸PHP发开框架FUGEN,继承组件medoo+plates,轻量级TP6框架替代品...
张福根一修品牌运营
Original
801 people have browsed it

自撸框架继承第三方库, 使用第三方类的成员为自己项目增砖添瓦:

自撸PHP发开框架

自撸PHP发开框架

一.APP项目架构

  • M:MODEL, 使用第三方包实现,composer require catfan/medoo
  • V:VIEW, 使用第三方包实现,composer require league/plates
  • C:CONTROLLER :业务逻辑是写在控制器中

二、开发流程:

1、创建自己的框架核心core, Model和View,继承第三方的组件:

Model ———————————
  1. <?php
  2. //框架核心 模型类
  3. namespace core;
  4. use Medoo\Medoo;
  5. class Model extends Medoo
  6. {
  7. public function __construct()
  8. {
  9. $options = [
  10. 'database_type' => 'mysql',
  11. 'database_name' => 'study',
  12. 'server' => 'localhost',
  13. 'username' => 'root',
  14. 'password' => '123456'
  15. ];
  16. // 继承父类构造方法
  17. parent::__construct($options);
  18. }
  19. }
View ———————————
  1. <?php
  2. //框架核心 视图类
  3. namespace core;
  4. use League\Plates\Engine;
  5. class View extends Engine
  6. {
  7. public $template;
  8. public function __construct($path)
  9. {
  10. $this->template = parent::__construct($path);
  11. }
  12. }

2、创建应用APP,MVC架构,创建应用的 models, views, controllers:

UsersController.php
  1. <?php
  2. namespace controllers;
  3. class UsersController
  4. {
  5. public $model;
  6. public $view;
  7. public function __construct($model,$view)
  8. {
  9. $this->model = $model;
  10. $this->view = $view;
  11. }
  12. public function index()
  13. {
  14. $num = 10;
  15. $page = $_GET['p'] ?? 1;
  16. $offset = ($page - 1) * $num ;
  17. //用户信息总数据
  18. $orders = $this->model->select('order',['id','name','pro','price'],['LIMIT'=>[$offset,$num]]);
  19. //数据表总数据条数
  20. $count = $this->model->count('order');
  21. //获取总页数
  22. $pages = ceil($count/ $num );
  23. //将数据渲染到模板上(模板赋值同步完成)
  24. return $this->view->render('user/list',["pages"=>$pages,"orders"=>$orders,"page"=>$page]);
  25. }
  26. }
UsersModel.php
  1. <?php
  2. namespace models;
  3. use core\Model;
  4. //用户自定义模型通常跟一张数据表对应
  5. class UsersModel extends Model
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. }
Views>user>list.php,不含分页代码

分页代码查看:PHP原生态!控制+模型+列表渲染+算法分页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>工人信息数据显示</title>
  7. <link rel="stylesheet" href="./public/static/css/style.css">
  8. </head>
  9. <body>
  10. <table>
  11. <caption>工人信息表</caption>
  12. <thead>
  13. <tr>
  14. <td>编号</td>
  15. <td>姓名</td>
  16. <td>技能</td>
  17. <td>价格</td>
  18. <td>操作</td>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <?php foreach($orders as $order):?>
  23. <tr>
  24. <td><?= $order['id']?></td>
  25. <td><?= $order['name']?></td>
  26. <td><?= $order['pro']?></td>
  27. <td><?= $order['price']?></td>
  28. <td>
  29. <button onclick="location.href='handle.php?action=edit&id=<?=$order['id']?>'">编辑</button>
  30. <button onclick="del(<?=$order['id']?>)">删除</button>
  31. </td>
  32. </tr>
  33. <?php endforeach;?>
  34. </tbody>
  35. </table>

3、应用入口页 index.php:

  1. <?php
  2. //composer 加载器
  3. require __DIR__ . '\\vendor\\autoload.php';
  4. use models\UsersModel;
  5. use core\View;
  6. use controllers\UsersController;
  7. //测试模型
  8. $model = new UsersModel;
  9. // var_dump($model);
  10. //测试视图
  11. $view = new View('app/views');
  12. // var_dump($view);
  13. //测试控制器
  14. $controller = new UsersController($model,$view);
  15. // var_dump($controller);
  16. print_r($controller->index()) ;

三、前页效果展示:

自撸PHP框架效果

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post