Blogger Information
Blog 45
fans 0
comment 0
visits 34581
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实现一个迷你的MVC小框架
咸鱼老爷
Original
689 people have browsed it

实现一个迷你的mvc

  • 1 安装第三方数据库框架medoo
    composer require catfan/medoo
  • 2 安装第三方php模板引擎plates
    composer require league/plates
  • 3 创建框架核心目录core
  • 4 创建框架模型类Model.php,使用构造函数连接数据库
    1. <?php
    2. namespace core;
    3. use Medoo\Medoo;
    4. class Model extends Medoo{
    5. public function __construct()
    6. {
    7. parent::__construct(
    8. [
    9. 'database_type'=>'mysql',
    10. 'database_name'=>'php',
    11. 'server'=>'localhost',
    12. 'username'=>'root',
    13. 'password'=>'123456',
    14. ]
    15. );
    16. }
    17. }
  • 5 创建框架视图类View.php
    1. <?php
    2. namespace core;
    3. use League\Plates\Engine;
    4. class View extends Engine{
    5. public $templates;
    6. public function __construct($path)
    7. {
    8. $this->templates=parent::__construct($path);
    9. }
    10. }
  • 6 创建mvc目录 app/controllers,app/models,app/views
  • 7 创建自定义模型类UserModel.php
    1. <?php
    2. namespace models;
    3. use core\Model;
    4. //自定义模型类通常与一张数据表绑定,继承自框架核心模型类
    5. class UserModel extends Model{
    6. public function __construct()
    7. {
    8. parent::__construct();
    9. }
    10. }
  • 8 创建控制器UserController.php
    1. <?php
    2. namespace controllers;
    3. class UserController{
    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. $user= $this->model->select('user',['id','name','gender','age','email','borthday','create_time']);
    19. //模板赋值
    20. return $this->view->render('users/list',['users'=>$user]);
    21. }
    22. }
  • 9 创建视图users/list.php
    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    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>Document</title>
    8. <style>
    9. .active{
    10. color: green;
    11. }
    12. a{
    13. text-decoration: none;
    14. color: #000;
    15. }
    16. </style>
    17. </head>
    18. <body>
    19. <table>
    20. <caption>员工列表</caption>
    21. <thead>
    22. <tr>
    23. <th>id</th>
    24. <th>姓名</th>
    25. <th>年龄</th>
    26. <th>性别</th>
    27. <th>邮箱</th>
    28. <th>生日</th>
    29. <th>入职日期</th>
    30. <th>操作</th>
    31. </tr>
    32. <tbody>
    33. <?php foreach($users as $v): ?>
    34. <tr>
    35. <td><?= $v['id'] ?></td>
    36. <td><?= $v['name'] ?></td>
    37. <td><?= $v['age'] ?></td>
    38. <td><?= $v['gender'] ?></td>
    39. <td><?= $v['email'] ?></td>
    40. <td><?= $v['borthday'] ?></td>
    41. <td><?= $v['create_time'] ?></td>
    42. <td><button>编辑</button> <button>删除</button></td>
    43. </tr>
    44. <?php endforeach?>
    45. </tbody>
    46. </thead>
    47. </table>
    48. <p>
    49. </p>
    50. </body>
    51. </html>
  • 10 配置composer.json文件 autoload psr-4自动映射
    1. {
    2. "name": "wang/php.cn",
    3. "description": "this is a test",
    4. "require": {
    5. "catfan/medoo": "^1.7",
    6. "league/plates": "^3.4"
    7. },
    8. "autoload": {
    9. "psr-4": {
    10. "models\\": "app/models",
    11. "views\\": "app/views",
    12. "controllers\\": "app/controllers",
    13. "core\\": "core"
    14. }
    15. }
    16. }
  • 11 使用compser -dump命令更新映射
  • 12 创建入口文件index.php
    ```
    <?php
    //入口文件

use controllers\UserController;
use core\View;
use models\UserModel;
require DIR.’/vendor/autoload.php’;
//模型
$model=new UserModel();
//视图
$view=new View(‘app/views’);
//控制器
$controller=new UserController($model,$view);
echo $controller->select();
```
浏览器访问入口文件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