批改状态:合格
老师批语:
实现一个迷你的mvc
composer require catfan/medoocomposer require league/plates
<?phpnamespace core;use Medoo\Medoo;class Model extends Medoo{public function __construct(){parent::__construct(['database_type'=>'mysql','database_name'=>'php','server'=>'localhost','username'=>'root','password'=>'123456',]);}}
<?phpnamespace core;use League\Plates\Engine;class View extends Engine{public $templates;public function __construct($path){$this->templates=parent::__construct($path);}}
<?phpnamespace models;use core\Model;//自定义模型类通常与一张数据表绑定,继承自框架核心模型类class UserModel extends Model{public function __construct(){parent::__construct();}}
<?phpnamespace controllers;class UserController{public $model;public $view;public function __construct($model,$view){$this->model=$model;$this->view=$view;}public function index(){return __METHOD__;}public function select(){//获取数据$user= $this->model->select('user',['id','name','gender','age','email','borthday','create_time']);//模板赋值return $this->view->render('users/list',['users'=>$user]);}}
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.active{color: green;}a{text-decoration: none;color: #000;}</style></head><body><table><caption>员工列表</caption><thead><tr><th>id</th><th>姓名</th><th>年龄</th><th>性别</th><th>邮箱</th><th>生日</th><th>入职日期</th><th>操作</th></tr><tbody><?php foreach($users as $v): ?><tr><td><?= $v['id'] ?></td><td><?= $v['name'] ?></td><td><?= $v['age'] ?></td><td><?= $v['gender'] ?></td><td><?= $v['email'] ?></td><td><?= $v['borthday'] ?></td><td><?= $v['create_time'] ?></td><td><button>编辑</button> <button>删除</button></td></tr><?php endforeach?></tbody></thead></table><p></p></body></html>
{"name": "wang/php.cn","description": "this is a test","require": {"catfan/medoo": "^1.7","league/plates": "^3.4"},"autoload": {"psr-4": {"models\\": "app/models","views\\": "app/views","controllers\\": "app/controllers","core\\": "core"}}}
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
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号