路由原理与实现

主流路由解决方案:pathinfo

  1. <?php
  2. require __DIR__ . '/../helper.php';
  3. //路由
  4. //!方案一,主流路由解决方案:pathinfo
  5. $url = 'http://php.edu/0507/router/demo2.php?c=user&a=hello&id=20&name=liyufeng';
  6. p(pathinfo($url));
  7. //显示如下:
  8. // Array
  9. // (
  10. // [dirname] => http://php.edu/0507/router
  11. // [basename] => demo2.php?c=user&a=hello&id=20&name=liyufeng
  12. // [extension] => php?c=user&a=hello&id=20&name=liyufeng
  13. // [filename] => demo2
  14. // )
  15. //!获取查询字符串:键值对:c=user&a=hello
  16. $url = 'http://php.edu/0507/router/demo2.php/one/two/three?c=user&a=hello';
  17. p(pathinfo($url));
  18. // $_SERVER['PATH_INFO']
  19. //获取参数:/one/two/three
  20. // p($_SERVER['PATH_INFO']);
  21. //单入口:index.php?m=模块,例如前台home,后台admin
  22. //模块/控制器/方法
  23. //index.php/module/controller/action
  24. //多入口
  25. // 前台:index.php,不需要模块,/controller/action
  26. // 后台:admin.php,不需要模块,/controller/action
  27. $url = 'http://php.edu/0507/router/demo2.php/admin/user/index';
  28. p($_SERVER['PATH_INFO']);
  29. //explode:把“/admin/user/index”转为数组
  30. p(explode('/',$_SERVER['PATH_INFO']));
  31. //第一种方法:array_filter过滤空数组
  32. // p(array_filter(explode('/',$_SERVER['PATH_INFO'])));
  33. //第一种方法:trim
  34. // p(explode('/', trim($_SERVER['PATH_INFO'],'/')));
  35. $request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
  36. //数组解构:将模块、控制器、方法都解析出来
  37. [$module,$controller,$action] = $request;
  38. //参数解析格式
  39. //按顺序传参
  40. //id = 1
  41. //name = admin
  42. $url = 'http://php.edu/0507/router/demo2.php/admin/user/index/1/admin';
  43. //按键名进行传参
  44. //id = 1
  45. //name = admin
  46. $url = 'http://php.edu/0507/router/demo2.php/admin/user/index/id/1/name/admin/qq';
  47. require __DIR__ . '/User.php';
  48. //调用: admin\User::index()
  49. //1.先获取类名
  50. $className = $module . '\\' . ucfirst($controller);
  51. p($className);
  52. //2.获取参数
  53. $params = array_splice($request,3);
  54. // //按顺序传参处理如下:
  55. // echo call_user_func_array([$className,$action],$params);
  56. //按键名进行传参还需要如下处理
  57. // p($params);
  58. //把索引数组转为关联数组
  59. //array_chunk():分组取出
  60. $arr = array_chunk($params,2);
  61. $result = [];
  62. foreach($arr as $item){
  63. [$key,$value] = $item;
  64. $result[$key] = $value;
  65. }
  66. p($result);
  67. //3.过滤空值
  68. $result = array_filter($result);
  69. p($result);
  70. //4.调用
  71. echo call_user_func_array([$className,$action],$result);

视图原理与实现

  1. <?php
  2. namespace phpcn;
  3. class View
  4. {
  5. //约定:控制器方法的模板,默认控制器为目录名,方法为文件名
  6. protected $controller;
  7. protected $action;
  8. protected $path;
  9. //模板容器
  10. protected $data = [];
  11. //初始化创建模板的路径
  12. public function __construct($controller,$action,$path='/view/')
  13. {
  14. $this->controller = $controller;
  15. $this->action = $action;
  16. $this->path = $path;
  17. }
  18. //模板赋值
  19. public function assign($name,$value)
  20. {
  21. //$name 是外部变量在模板文件中的变量名
  22. //$value 模板变量的值
  23. $this->data[$name] = $value;
  24. }
  25. //模板渲染
  26. public function render($path = '')
  27. {
  28. //展开模板变量数组
  29. extract($this->data);
  30. if(empty($path)){
  31. //生成路径
  32. $file = __DIR__ . $this->path . $this->controller . '/' . $this->action . '.php';
  33. }else{
  34. $file = $path;
  35. }
  36. //加载
  37. // include $file or die('视图不存在');
  38. file_exists($file) ? include $file : die('视图不存在');
  39. }
  40. }
  41. //调用
  42. $controller = 'User';
  43. $action = 'hello';
  44. $view = new View($controller,$action);
  45. //模板赋值:变量
  46. $view->assign('username','李先生');
  47. //模板赋值:数组
  48. $items = [
  49. ['name'=>'李龙','gongzi'=>5000],
  50. ['name'=>'刘强','gongzi'=>6000],
  51. ['name'=>'杨旭','gongzi'=>7000],
  52. ];
  53. $view->assign('items',$items);
  54. //渲染模板
  55. $view->render();

数据库操作:增删查改

  1. <?php
  2. namespace phpcn;
  3. use PDO;
  4. class Db
  5. {
  6. protected $db;
  7. protected $table;
  8. protected $field;
  9. protected $limit;
  10. protected $opt = [];
  11. public function __construct($dsn,$username,$password)
  12. {
  13. $this->db = new PDO($dsn, $username, $password);
  14. }
  15. public function table($table){
  16. $this->table = $table;
  17. return $this;
  18. }
  19. public function field($field){
  20. $this->field = $field;
  21. return $this;
  22. }
  23. public function limit($limit = 10){
  24. $this->limit = $limit;
  25. $this->opt['limit'] = " LIMIT $limit";
  26. return $this;
  27. }
  28. public function page($page = 1)
  29. {
  30. //偏移量:offset = (page - 1) * limit
  31. $this->opt['offset'] = ' OFFSET ' . ($page - 1) * $this->limit;
  32. return $this;
  33. }
  34. //查询条件
  35. public function where($where = '')
  36. {
  37. $this->opt['where'] = " WHERE $where";
  38. return $this;
  39. }
  40. //查询
  41. public function select()
  42. {
  43. //拼装sql
  44. $sql = 'SELECT ' . $this->field . ' FROM ' . $this->table;
  45. $sql .= $this->opt['where'] ?? null;
  46. $sql .= $this->opt['limit'] ?? null;
  47. $sql .= $this->opt['offset'] ?? null;
  48. echo $sql . '<br>';
  49. //预处理
  50. $stmt = $this->db->prepare($sql);
  51. $stmt->execute();
  52. //清空条件
  53. $this->opt['where'] = null;
  54. return $stmt->fetchAll();
  55. }
  56. //插入
  57. public function insert($data)
  58. {
  59. // [a=>1,b=2] => 'a=1,b=2'
  60. $str ='';
  61. foreach($data as $key=>$value){
  62. $str .= $key . '="' . $value . '",';
  63. }
  64. $sql = 'INSERT ' . $this->table . ' SET ' . rtrim($str,',');
  65. echo $sql .'<br>';
  66. //预处理
  67. $stmt = $this->db->prepare($sql);
  68. $stmt->execute();
  69. //清空条件
  70. $this->opt['where'] = null;
  71. return $stmt->rowCount();
  72. }
  73. //更新
  74. public function update($data)
  75. {
  76. // [a=>1,b=2] => 'a=1,b=2'
  77. $str ='';
  78. foreach($data as $key=>$value){
  79. $str .= $key . '="' . $value . '",';
  80. }
  81. $sql = 'UPDATE ' . $this->table . ' SET ' . rtrim($str,',');
  82. echo $sql .'<br>';
  83. //禁止无条件更新
  84. $sql .= $this->opt['where'] ?? die('禁止无条件更新');
  85. //预处理
  86. $stmt = $this->db->prepare($sql);
  87. $stmt->execute();
  88. //清空条件
  89. $this->opt['where'] = null;
  90. return $stmt->rowCount();
  91. }
  92. //删除
  93. public function delete()
  94. {
  95. $sql = 'DELETE FROM ' . $this->table;
  96. echo $sql .'<br>';
  97. //禁止无条件更新
  98. $sql .= $this->opt['where'] ?? die('禁止无条件删除');
  99. //预处理
  100. $stmt = $this->db->prepare($sql);
  101. $stmt->execute();
  102. //清空条件
  103. $this->opt['where'] = null;
  104. return $stmt->rowCount();
  105. }
  106. }
  107. //测试
  108. //查询
  109. $db = new Db('mysql:dbname=phpedu','root','phpedu');
  110. // $result = $db->table('staff')->field('id,name,email')->select();
  111. // $result = $db->table('staff')
  112. // ->field('id,name,email')
  113. // ->where('id > 1')
  114. // ->limit(2)
  115. // ->page(2)
  116. // ->select();
  117. // require __DIR__ . '/helper.php';
  118. // p($result);
  119. //新增
  120. // $n = $db->table('staff')->insert(['name'=>'李先生','sex'=>0,'email'=>'li@qq.com']);
  121. // 打印结果:INSERT staff SET name=李先生,email=li@qq.com,sex=0,
  122. // echo $n > 0 ? '新增成功' . $n . '条<br>' : '新增失败'. $n . '条<br>';
  123. //更新
  124. // $n = $db->table('staff')->where('id = 18')->update(['name'=>'刘女士','sex'=>1]);
  125. // echo $n > 0 ? '更新成功' . $n . '条<br>' : '更新失败'. $n . '条<br>';
  126. //删除
  127. $n = $db->table('staff')->where('id = 14')->delete();
  128. echo $n > 0 ? '删除成功' . $n . '条<br>' : '删除失败'. $n . '条<br>';

更多相关文章

  1. 常用数组函数学习
  2. android java开发 第一天 之熟悉eclipse adt
  3. Android中的数据结构解析(四)SparseArray和ArrayMap
  4. 20172323 2017-2018-2《程序设计与数据结构》第十一周学习总结
  5. PHP中的extract()函数
  6. PHP中的视图基类的实例演示
  7. 5个数组函数实例演示
  8. Linux/Android——input_handler之evdev (四)
  9. 2020年20种最佳Android应用程序模板

随机推荐

  1. Android上的命令内存相关
  2. android操作xml
  3. Android内核的根文件系统
  4. Android 常用布局
  5. Android动画一
  6. Android常见错误解析
  7. android 技术
  8. Android组件的样式
  9. Android进程和线程
  10. AndroidManifest.xml - 【 manifest -> p