Route.php 1.8 KB

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