博客列表 >12月06日练习迷你的MVC框架-九期线上班

12月06日练习迷你的MVC框架-九期线上班

WJF
WJF原创
2019年12月13日 18:53:14636浏览

迷你的MVC框架


Db.php
  1. <?php
  2. //命名空间
  3. namespace mvc;
  4. //创建类
  5. class Db{
  6. //配置数据库信息
  7. private $db = [
  8. 'type' => 'mysql',
  9. 'host' => '127.0.0.1',
  10. 'dbname' => 'phpcn',
  11. 'username' => 'root',
  12. 'password' => 'root66',
  13. ];
  14. public static $pdo;
  15. //构造方法
  16. private function __construct(...$params)
  17. {
  18. $dsn = $this->db['type'] . ':host=' .$this->db['host'] . ';dbname=' . $this->db['dbname'];
  19. $username = $this->db['username'];
  20. $password = $this->db['password'];
  21. try{
  22. self::$pdo = new \PDO($dsn,$username,$password);
  23. // echo '测试连接成功';
  24. }catch (\PDOException $e){
  25. die('数据库失败 错误信息:' . $e->getMessage());
  26. }
  27. }
  28. //单例模式判断重复连接
  29. public static function connect(...$params){
  30. //判断是否已经实例化
  31. if (is_null(self::$pdo)){
  32. new self(...$params);
  33. }
  34. return self::$pdo;
  35. }
  36. //禁止外部克隆方法
  37. private function __clone()
  38. {
  39. // ****
  40. }
  41. // //新增,更新,删除数据库操作
  42. // public static function exec($sql){
  43. // $stmt = self::$pdo->prepare($sql);
  44. // $stmt->execute();
  45. // }
  46. //获取单条数据
  47. public static function fetch($sql){
  48. $stmt = self::$pdo->prepare($sql);
  49. $stmt->execute();
  50. return $stmt->fetch(\PDO::FETCH_ASSOC);
  51. }
  52. //获取多条数据
  53. public static function fetchAll($sql){
  54. $stmt = self::$pdo->prepare($sql);
  55. $stmt->execute();
  56. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  57. }
  58. }
  59. //new Db();

View.php
  1. <?php
  2. namespace mvc;
  3. class View{
  4. //获取多条数据
  5. public function fetch($data){
  6. $table = '<style>
  7. table {border-collapse: collapse; border: 1px solid; width: 65%;margin:50px auto;}
  8. caption {font-size: 1.8rem; margin-bottom: 20px;}
  9. tr {height: 40px;}
  10. tr:first-of-type { background-color:lightblue;}
  11. td,th {border: 1px solid}
  12. td{text-align: center}
  13. td:first-of-type {text-align: center}
  14. </style>';
  15. $table .= '<table>';
  16. $table .= '<caption>用户信息表<form action="index.php" method="get">
  17. 指定id:<input type="text" name="id">
  18. <button>查询</button>
  19. </form></caption>';
  20. $table .= '<tr><th>ID</th><th>用户名</th><th>密码</th><th>注册时间</th><th>IP</th><th>操作</th></tr>';
  21. //循环数组
  22. foreach ($data as $v) {
  23. $table .= '<tr>';
  24. $table .= '<td>' . $v['id'] . '</td>';
  25. $table .= '<td>' . $v['name'] . '</td>';
  26. $table .= '<td>' . '无权查看' . '</td>';
  27. $table .= '<td>' . date('Y-m-s h:i:s',$v['time']) . '</td>';
  28. $table .= '<td>' . $v['ip'] . '</td>';
  29. $table .= "<td><a href='index.php?id={$v['id']}'>查看</a> </td>";
  30. $table .= '</tr>';
  31. }
  32. //date('Y-m-s h:i:s',time())
  33. $table .= '</table>';
  34. return $table;
  35. }
  36. //获取单条数据
  37. public function find($data){
  38. $table = '<style>
  39. table {border-collapse: collapse; border: 1px solid; width: 65%;margin:50px auto;}
  40. caption {font-size: 1.8rem; margin-bottom: 20px;}
  41. tr {height: 40px;}
  42. tr:first-of-type { background-color:indianred;}
  43. td,th {border: 1px solid}
  44. td{text-align: center}
  45. td:first-of-type {text-align: center}
  46. </style>';
  47. $table .= '<table>';
  48. $table .= '<caption>用户详情表</caption>';
  49. $table .= '<tr><th>ID</th><th>用户名</th><th>密码</th><th>注册时间</th><th>IP</th></tr>';
  50. $table .= '<tr>';
  51. $table .= '<td>' . $data['id'] . '</td>';
  52. $table .= '<td>' . $data['name'] . '</td>';
  53. $table .= '<td>' . $data['pass'] . '</td>';
  54. $table .= '<td>' . date('Y-m-s h:i:s',$data['time']) . '</td>';
  55. $table .= '<td>' . $data['ip'] . '</td>';
  56. $table .= '</tr>';
  57. return $table;
  58. }
  59. }
Model.php
  1. <?php
  2. //命名空间
  3. namespace mvc;
  4. //引入Db文件
  5. require __DIR__ . '/Db.php';
  6. class Model{
  7. public $data;
  8. // 构造方法 连接数据库
  9. public function __construct()
  10. {
  11. Db::connect();
  12. }
  13. //获取单条数据
  14. public function get($id){
  15. $sql = "SELECT * FROM `user` WHERE id= {$id}";
  16. $this->data = Db::fetch($sql);
  17. return $this->data;
  18. }
  19. //获取多条数据
  20. public function getAll(){
  21. $sql = "SELECT * FROM `user`";
  22. $this->data = Db::fetchAll($sql);
  23. return $this->data;
  24. }
  25. }

index.php
  1. <?php
  2. namespace mvc;
  3. require __DIR__ . '/Model.php';
  4. require __DIR__ . '/View.php';
  5. //创建服务容器
  6. class Container{
  7. public $int = [];
  8. public function bind($alias,\Closure $process){
  9. $this->int[$alias] = $process;
  10. }
  11. public function make($alias, $params = []){
  12. return call_user_func_array($this->int[$alias],$params);
  13. }
  14. }
  15. $container = new Container();
  16. $container->bind('model',function (){
  17. return new Model();
  18. });
  19. $container->bind('view',function (){
  20. return new View();
  21. });
  22. //门面模式
  23. class Facade{
  24. //接受实例化容器
  25. protected static $container;
  26. //绑定数据
  27. protected static $data;
  28. protected static $id;
  29. public static function initialize(Container $container){
  30. static::$container = $container;
  31. }
  32. public static function getAll(){
  33. static::$data = static::$container->make('model')->getAll();
  34. }
  35. //查询单条数据
  36. public static function get($id){
  37. static::$id = static::$container->make('model')->get($id);
  38. }
  39. public static function fetch(){
  40. return static::$container->make('view')->fetch(static::$data);
  41. }
  42. public static function find(){
  43. return static::$container->make('view')->find(static::$id);
  44. }
  45. }
  46. //创建控制器
  47. class Controller{
  48. public function __construct(Container $container)
  49. {
  50. Facade::initialize($container);
  51. }
  52. //多条数据
  53. public function getAll(){
  54. Facade::getAll();
  55. return Facade::fetch();
  56. }
  57. //调用单条数据
  58. public function find()
  59. {
  60. //获取数据
  61. Facade::get($_GET['id']);
  62. //渲染模板
  63. return Facade::find();
  64. // return print_r($_GET['id'],true);
  65. }
  66. }
  67. $controller = new Controller($container);
  68. //echo $controller->getAll();
  69. if (is_null($_GET['id'])){
  70. echo $controller->getAll();
  71. }else{
  72. echo $controller->find();
  73. }

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