Route.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace daswork\lib;
  3. use daswork\lib\Config;
  4. class Route
  5. {
  6. public $model; # 模块
  7. public $ctrl; # 控制器
  8. public $action; #方法
  9. public function __construct()
  10. {
  11. //xx.com/index.php/index/index
  12. //xx.com/index/index
  13. /**
  14. * 1.隐藏index.php文件,引入.htaccess文件,与入口文件同级
  15. * 2.获取URL 参数部分,即get传值
  16. * 3.返回对应模块,控制器和方法 REQUEST_URI
  17. */
  18. $route = Config::get('route');
  19. if (isset($_SERVER['PATH_INFO'])&& $_SERVER['PATH_INFO'] != '/') {
  20. $path = $_SERVER['PATH_INFO'];
  21. $pathArr = explode('/', trim($path, '/'));//数组
  22. // echo json_encode($pathArr);
  23. // exit;
  24. if (isset($pathArr[0]) && !empty($pathArr[0])) {
  25. $this->model = $pathArr[0];
  26. unset($pathArr[0]);
  27. } else {
  28. $this->model = $route['m'];
  29. }
  30. if (isset($pathArr[1])) {
  31. $this->ctrl = ucfirst(camelize($pathArr[1]));
  32. unset($pathArr[1]);
  33. } else {
  34. $this->ctrl = $route['c'];
  35. }
  36. if (isset($pathArr[2])) {
  37. $this->action = camelize($pathArr[2]);
  38. unset($pathArr[2]);
  39. } else {
  40. $this->action = $route['a'];
  41. }
  42. //url 多余部分转换成GET eg:index/index/index/id/1 实现get传值
  43. // var_dump($pathArr);
  44. $count = count($pathArr) + 3;
  45. $i = 3;
  46. while ($i < $count) {
  47. if (isset($pathArr[$i+1])) {
  48. $_GET[$pathArr[$i]] = $pathArr[$i+1];
  49. }
  50. $i = $i + 2;
  51. };
  52. } else {
  53. $this->model = $route['m'];
  54. $this->ctrl = $route['c'];
  55. $this->action = $route['a'];
  56. }
  57. $GLOBALS['model'] = $this->model;
  58. $GLOBALS['ctrl'] = $this->ctrl;
  59. $GLOBALS['action'] = $this->action;
  60. }
  61. }