博客列表 >12月6日_自己实现一个小型mvc框架 - 九期线上班

12月6日_自己实现一个小型mvc框架 - 九期线上班

只猫
只猫原创
2019年12月13日 18:26:26679浏览

使用mvc做页面展示

数据表customer

目录

Db.php

  1. <?php
  2. namespace mvc1\model;
  3. use PDO;
  4. class Db
  5. {
  6. private $dbConfig = [
  7. 'db'=>'mysql',
  8. 'host'=>'localhost',
  9. 'username'=>'root',
  10. 'password'=>'root',
  11. 'dbname'=>'mydb'
  12. ];
  13. private static $instance = null;
  14. private $conn = null;
  15. private function __construct($params=[]){
  16. //初始化参数
  17. $this->dbConfig = array_merge($this->dbConfig,$params);
  18. //连接数据库
  19. $this->connect();
  20. }
  21. //禁止克隆
  22. private function __clone(){
  23. }
  24. //创建唯一实例
  25. public static function getInstance($params){
  26. if(!self::$instance instanceof self){
  27. self::$instance = new self($params);
  28. }
  29. return self::$instance;
  30. }
  31. private function connect(){
  32. //连接数据库
  33. try{
  34. $dsn = "{$this->dbConfig['db']}:host={$this->dbConfig['host']};dbname={$this->dbConfig['dbname']}";
  35. //PDO连接对象
  36. $this->conn = new PDO($dsn,$this->dbConfig['username'],$this->dbConfig['password']);
  37. }catch(PDOException $e){
  38. die('数据库连接失败'.$e->getMessage());
  39. }
  40. }
  41. //查询全部数据
  42. public function selectAll($sql){
  43. return $this->conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  44. }
  45. }

Model.php

  1. <?php
  2. namespace mvc1\model;
  3. include 'Db.php';
  4. class Model
  5. {
  6. protected $db = null;
  7. public $data = null;
  8. public function __construct(){
  9. $this->init();
  10. }
  11. private function init(){
  12. $dbConfig = [
  13. 'username'=>'root',
  14. 'password'=>'12341234',
  15. 'dbname'=>'php_2019',
  16. ];
  17. //Db类实例
  18. $this->db = Db::getInstance($dbConfig);
  19. }
  20. //获取数据表全部数据
  21. public function getAll($tableName){
  22. $sql = "SELECT * FROM .$tableName;";
  23. return $this->data = $this->db->selectAll($sql);
  24. }
  25. }

CustomerModel.php

  1. <?php
  2. namespace mvc1\model;
  3. require 'Model.php';
  4. class CustomerModel extends Model
  5. {
  6. private $cid;
  7. private $name;
  8. private $password;
  9. private $sex;
  10. private $birth;
  11. private $level;
  12. private $is_vip;
  13. private $createtime;
  14. private $updatetime;
  15. private $status;
  16. private $del;
  17. public function __get($field){
  18. return $this->$field;
  19. }
  20. public function __set($field,$value){
  21. $this->$field = $value;
  22. }
  23. public function __construct(){
  24. parent::__construct();
  25. //自动转换 时间日期
  26. $this->birth = date('Y-m-d' , $this->birth);
  27. $this->createtime = date('Y-m-d' , $this->createtime);
  28. $this->updatetime = date('Y-m-d' , $this->updatetime);
  29. //自动转换 性别
  30. if($this->sex == 1 || $this->sex == 0){
  31. $this->sex = $this->sex ? '男' : '女';
  32. }else{
  33. $this->sex = '未知';
  34. }
  35. }
  36. }

CustomerController.php

  1. <?php
  2. namespace mvc1\controller;
  3. use mvc1\model\CustomerModel;
  4. use mvc1\view\CustomerView;
  5. class CustomerController
  6. {
  7. protected $customerModel;
  8. protected $customerView;
  9. public function __construct(customerModel $customerModel,customerView $customerView){
  10. $this->customerModel = $customerModel;
  11. $this->customerView = $customerView;
  12. }
  13. //首页
  14. public function index(){
  15. $data = $this->customerModel->getAll('customer');
  16. return $this->customerView->show($data);
  17. }
  18. }

CustomerView.php

  1. <?php
  2. namespace mvc1\view;
  3. class CustomerView
  4. {
  5. public function show($data){
  6. $table = '<table>';
  7. $table .= '<caption>客户信息表</caption>';
  8. $table .= '<tr><th>ID</th><th>客户</th><th>性别</th><th>生日</th><th>等级</th><th>vip</th><th>创建时间</th></tr>';
  9. foreach ($data as $customer){
  10. $table .='<tr>';
  11. $table .='<td>'. $customer['cid'] .'</td>';
  12. $table .='<td>'. $customer['name'] .'</td>';
  13. $table .='<td>'. $customer['sex'] .'</td>';
  14. $table .='<td>'. date('Y-m-d' ,$customer['birth']) .'</td>';
  15. $table .='<td>'. $customer['level'] .'</td>';
  16. $table .='<td>'. $customer['is_vip'] .'</td>';
  17. $table .='<td>'. date('Y-m-d' ,$customer['createtime']) .'</td>';
  18. $table .='</tr>';
  19. }
  20. $table .= '</table>';
  21. return $table;
  22. }
  23. }
  24. echo '
  25. <style>
  26. table {
  27. border-collapse: collapse;
  28. border: 1px solid;
  29. width: 500px;
  30. height: 150px;
  31. margin:auto;
  32. }
  33. caption {
  34. font-size:1.2rem;
  35. margin-bottom: 10px;
  36. }
  37. tr:first-of-type{
  38. background-color:lightblue;
  39. }
  40. td,th{
  41. border:1px solid
  42. }
  43. td:first-of-type{
  44. text-align:center;
  45. }
  46. </style>
  47. ';

autoload.php

  1. <?php
  2. spl_autoload_register(function($className){
  3. $path = str_replace('\\', '/', $className);
  4. require dirname(__DIR__).DIRECTORY_SEPARATOR.$path.'.php';
  5. });

index.php

  1. <?php
  2. require __DIR__.'/autoload.php';
  3. use mvc1\model\CustomerModel;
  4. use mvc1\view\CustomerView;
  5. use mvc1\controller\CustomerController;
  6. //客户端调用
  7. $customerModel = new CustomerModel();
  8. $customerView = new CustomerView();
  9. $customerController = new CustomerController($customerModel,$customerView);
  10. echo $customerController->index();

页面展示

总结:自己要写一个小框架给自己用还是挺难实现的。脑子里有想法又很乱。只做了基于mvc的展示,很多功能都没有做。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议