批改状态:合格
老师批语:
Model: composer require catfan/medoo 得到一个 vendor/catfan/medoo文件View: composer require league/plates 得到一个 vendor/league/plates文件
{"name": "zhu/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"}}}
<?php// 模型类namespace core;use Medoo\Medoo;class Model extends Medoo{public function __construct(){parent::__construct(['database_type' => 'mysql','database_name' => 'phpedu','server' => 'localhost','username' => 'root','password' => 'root',]);}public function first(){// 自定义方法}}
<?phpnamespace core;use League\Plates\Engine;class View extends Engine{public $templates;public function __construct($path){$templates = parent::__construct($path);}}
<?phpnamespace controllers;class StaffsController{public $model;public $view;public function __construct($model, $view){$this->model = $model;$this->view = $view;}public function index(){return __METHOD__;}public function select(){// 获取数据$staffs = $this->model->select('staffs', ['id', 'name', 'gender', 'salary', 'email'], ['salary[>=]' => 6000, 'LIMIT' => 6]);// 模板赋值return $this->view->render('staffs/list', ['staffs' => $staffs]);}}
<?phpnamespace models;use core\Model;// 自定义模型类通常与一张数据表绑定,继承自框架核心模型类class StaffsModel extends Model{public function __construct(){parent::__construct();}}
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>员工信息</title><style>body {display: flex;flex-direction: column;align-items: center;}table {border-collapse: collapse;border: 1px solid;width: 50%;text-align: center;}th,td {border: 1px solid;padding: 5px;}tr:first-child {background-color: #0ff;}</style></head><body><h3>用户管理系统</h3><table><tr><th>id</th><th>姓名</th><th>性别</th><th>工资</th><th>邮箱</th><th>操作</th></tr><?php foreach ($staffs as $uname): ?><tr><td><?=$this->e($uname['id'])?></td><td><?=$this->e($uname['name'])?></td><td><?=$this->e($uname['gender']) == 'male' ? '男' : '女'?></td><td><?=$this->e($uname['salary'])?></td><td><?=$this->e($uname['email'])?></td><td><button>编辑</button> <button>删除</button></td></tr><?php endforeach ?></table><p><a href="">1</a><a href="">2</a><a href="">3</a><a href="">4</a><a href="">5</a><a href="">6</a></p></body></html>
{"name": "zhu/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"}}}
<?php// 入口文件use models\StaffsModel;use controllers\StaffsController;use core\View;require __DIR__ . '/vendor/autoload.php';// 测试模型$model = new StaffsModel();// 测试视图$view = new View('app/views');// 测试控制器$controller = new StaffsController($model,$view);print_r($controller->select());

第一步先安装配置好 composer 下载地址: https://pkg.phpcomposer.com/第二步找到composer所需的第三方包依赖进行快速搭建模型Model与视图View第三步 创建一个核心目录文件夹第四步 创建app目录,搭建起MVC架构第五步 配置好composer.js文件,用来映射目录文件index.php 是入口文件得注意use导入名称信息
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号