Blogger Information
Blog 47
fans 1
comment 0
visits 53078
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP - 路由原理
晴天
Original
801 people have browsed it

1. 路由原理

目标:将URL中的控制器,方法解析出来,映射到对应的控制器类和方法上

  1. <?php
  2. //创建控制器
  3. class UserController
  4. {
  5. // 创建方法
  6. public function getUser($id, $name)
  7. {
  8. // 直接返回数据
  9. return "id ===> $id , name ===> $name";
  10. }
  11. }
  12. //http://php11.edu/0514/index.php/控制器/方法/参数1/值1/参数2/值2/
  13. //http://php11.edu/0514/index.php/user/getuser/id/200/name/admin
  14. //1.先拿到$_SERVER['PATH_INFO']数据
  15. //$pathinfo = $_SERVER['PATH_INFO'];
  16. //echo $pathinfo; // 输出 /user/getuser/id/200/name/admin
  17. //2.将字符串利用‘/’切割 转为数组
  18. //$pathinfo = explode('/', $pathinfo);
  19. //print_r($pathinfo);
  20. //输出
  21. //Array
  22. //(
  23. //[0] =>
  24. //[1] => user
  25. //[2] => getuser
  26. //[3] => id
  27. //[4] => 200
  28. //[5] => name
  29. //[6] => admin
  30. //)
  31. //3.将数组中的空值移除
  32. //$pathinfo = array_filter($pathinfo);
  33. //print_r($pathinfo);
  34. //输出
  35. //Array
  36. //(
  37. //[1] => user
  38. //[2] => getuser
  39. //[3] => id
  40. //[4] => 200
  41. //[5] => name
  42. //[6] => admin
  43. //)
  44. // 4. 将数组键值进行重排
  45. //$pathinfo = array_values($pathinfo);
  46. //print_r($pathinfo);
  47. //输出
  48. //Array
  49. //(
  50. //[0] => user
  51. //[1] => getuser
  52. //[2] => id
  53. //[3] => 200
  54. //[4] => name
  55. //[5] => admin
  56. //)
  57. ### 将前几步组合到一起
  58. $pathinfo = array_values(array_filter(explode('/',$_SERVER['PATH_INFO'])));
  59. //print_r($pathinfo); // 输出值与第四步相同
  60. //获取控制器 去除数组中的第一个值并进行首字母大写
  61. $controller = ucfirst(array_shift($pathinfo)).'Controller';
  62. //echo $controller; //输出 UserController
  63. //获取方法 注意方法不区分大小写
  64. $action = array_shift($pathinfo);
  65. //echo $action; // 输出 getuser
  66. //获取参数 当前数组中仅剩键值 使用for循环 获取
  67. //print_r($pathinfo);
  68. //输出
  69. //Array
  70. //(
  71. //[0] => id
  72. //[1] => 200
  73. //[2] => name
  74. //[3] => admin
  75. //)
  76. $params = [];
  77. for ($i = 0 ; $i <= count($pathinfo) ; $i+=2){
  78. // 判断如果当前键值对的下一个键值对存在则执行
  79. if (isset($pathinfo[$i+1])){
  80. $params[$pathinfo[$i]] = $pathinfo[$i+1];
  81. }
  82. }
  83. //打印一下数组
  84. //print_r($params);
  85. //输出
  86. //Array
  87. //(
  88. // [id] => 200
  89. // [name] => admin
  90. //)
  91. //开始调用
  92. //实例化控制器
  93. $user = new $controller;
  94. //调用方法并传值
  95. echo $user->$action(...array_values($params));
  96. //输出 id ===> 200 , name ===> admin
  97. // 成功获取到值
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:路由的形式非常多, 而且是http请求到达控制器的第一步, 是整个网站的哨兵
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post