Blogger Information
Blog 47
fans 3
comment 0
visits 38101
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP迷你MVC小框架实现步骤
Original
721 people have browsed it

迷你MVC小框架文件目录结构

  • app 应用目录
    • controller 控制器目录
    • models 模型目录
    • views 视图目录
  • core 核心目录

实现步骤

1.安装第三方包catfan/medoo与league/plates

  • catfan/medoo 数据库框架:composer require catfan/medoo
  • league/plates 模板框架: composer require league/plates

2.core目录新建Model与View文件

  • 模型类:Model.php

    1. namespace core;
    2. use Medoo\Medoo;
    3. class Model extends Medoo
    4. {
    5. public function __construct()
    6. {
    7. parent::__construct([
    8. 'database_type' => 'mysql',
    9. 'database_name' => 'phpedu',
    10. 'server' => 'localhost',
    11. 'username' => 'root',
    12. 'password' => '123456'
    13. ]);
    14. }
    15. public function first() {
    16. // 自定义方法
    17. }
    18. }
  • 视图类:View.php

    1. namespace core;
    2. use League\Plates\Engine;
    3. class View extends Engine
    4. {
    5. public $templates;
    6. public function __construct($path)
    7. {
    8. $templates = parent::__construct($path);
    9. }
    10. }

3.在app目录下新建controllers models views 目录

  • controllers目录:新建UserController.php文件,获取用户数据赋值给模板
  1. namespace controllers;
  2. class UserController
  3. {
  4. public $model;
  5. public $view;
  6. public function __construct($model,$view)
  7. {
  8. $this->model = $model;
  9. $this->view = $view;
  10. }
  11. public function index()
  12. {
  13. return __METHOD__;
  14. }
  15. public function select()
  16. {
  17. // 获取数据
  18. $users = $this->model->select('users',['sid','name','gender','salary','email'],['salary[>=]'=>5000,'LIMIT'=> 8]);
  19. // 模板赋值
  20. return $this->view->render('users/list',['users'=> $users]);
  21. }
  22. }
  • Model目录:新建UserModel.php获取用户表数据
  1. namespace models;
  2. use core\Model;
  3. class UserModel extends Model
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. }
  • View目录:新建目录users 在新建目录下创建list.php文件显示用户数据视图
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>用户信息</title>
  8. <style>
  9. body {
  10. display: flex;
  11. flex-direction: column;
  12. align-items: center;
  13. }
  14. table {
  15. border-collapse: collapse;
  16. border: 1px solid;
  17. width: 50%;
  18. text-align: center;
  19. }
  20. th,
  21. td {
  22. border: 1px solid;
  23. padding: 5px;
  24. }
  25. tr:first-child {
  26. background-color: lightcoral;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <h3>用户管理系统</h3>
  32. <table>
  33. <tr>
  34. <th>UID</th>
  35. <th>用户名</th>
  36. <th>性别</th>
  37. <th>工资</th>
  38. <th>邮箱</th>
  39. <th>操作</th>
  40. </tr>
  41. <?php foreach ($users as $user): ?>
  42. <tr>
  43. <td><?=$this->e($user['sid'])?></td>
  44. <td><?=$this->e($user['name'])?></td>
  45. <td><?=$this->e($user['gender']) == 'male' ? '男' : '女'?></td>
  46. <td><?=$this->e($user['salary'])?></td>
  47. <td><?=$this->e($user['email'])?></td>
  48. <td><button>编辑</button><button>删除</button></td>
  49. </tr>
  50. <?php endforeach ?>
  51. </table>
  52. <p>
  53. <a href="">1</a>
  54. <a href="">2</a>
  55. <a href="">3</a>
  56. </p>
  57. </body>
  58. </html>

3.composer将控件映射到目录

  1. {
  2. "require": {
  3. "catfan/medoo": "^1.7",
  4. "league/plates": "^3.4"
  5. },
  6. "autoload": {
  7. "psr-4": {
  8. "models\\": "app/models",
  9. "views\\": "app/views",
  10. "controllers\\": "app/controllers",
  11. "core\\": "core"
  12. }
  13. }
  14. }
  • 修改完成在终端运行composer dump更新自动加载

4.index.php入口文件

  1. // 入口文件
  2. use controllers\UserController;
  3. use models\UserModel;
  4. use core\View;
  5. require __DIR__ . '/vendor/autoload.php';
  6. // 模型
  7. $model = new UserModel();
  8. // 视图
  9. $view = new View('app/views');
  10. // 控制器
  11. $controller = new UserController($model,$view);
  12. print_r($controller->select());

运行结果截图:


总结:
1.使用composer第三方包依赖快速创建Model与View
2.创建core核心文件目录
3.创建App目录,严格按照MVC架构创建三个文件夹Models、Views、Controllers
4.composer.js映射autoload目录时需注意在其他文件use导入时的名称

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