Blogger Information
Blog 29
fans 1
comment 0
visits 23237
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP路由器route初步认识
阿心
Original
1241 people have browsed it

查询GET字符串方式

  1. <?php
  2. // 查询GET字符串
  3. //如:http://127.0.0.7/route.php?action=edit&id=10&name=admin
  4. //方法一:
  5. if($_GET):
  6. vprintf('?action=%s&id=%s&name=%s',$_GET);
  7. endif;
  8. echo '<hr>';
  9. //方法二,
  10. //中文:查询_字符串
  11. echo $_SERVER['QUERY_STRING'];
  12. echo '<hr>';
  13. //查询路径参数
  14. //如:http://127.0.0.7/route.php/action/edit/id/10/name/admin
  15. //方法:PATH_INFO(路径_信息)
  16. echo $_SERVER['PATH_INFO'];
  17. echo '<hr>';
  18. //path_info()函数。跟路径变量不同,返回的是路径信息不是变量
  19. print_r(pathinfo('0514/route.php'));
  20. echo '<hr>';
  21. //$_SERVER['REQUEST_URI'] 统一资源定位符(可以是GET,也可以PATH_INFO;
  22. echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  23. echo '<hr>';
  24. //http://域名/控制器/方法/参数/值/.....(这里要特别注意:控制器名称和类名称对应。方法名称和类中方法名称也是对应的)
  25. class Action
  26. {
  27. public $id;
  28. public $name;
  29. public function edit($id,$name)
  30. {
  31. $this->id = $id;
  32. $this->name = $name;
  33. }
  34. }
  35. //1,过滤空数组arra_filter
  36. //array_values() 返回 input 数组中所有的值并给其建立数字索引
  37. $pathinfo=array_filter(array_filter(explode('/',$_SERVER['PATH_INFO'])));
  38. //print_r($pathinfo);
  39. //,2,解析控制器 。初始数据
  40. //$user=$pathinfo['0'];
  41. $user=ucfirst(array_shift($pathinfo));
  42. //ucfirst首字母大写
  43. //echo $user;
  44. //3,解析方法同上
  45. $edit=array_shift($pathinfo);
  46. //echo $edit;
  47. //parse_str — 将字符串解析成多个变量
  48. //,4,解析参数 //如果是PATH_INF这里改掉就可以了
  49. parse_str($_SERVER['PATH_INFO'],$params);
  50. //print_r($params);
  51. //5,调用控制器==>已经解析到(2方法)
  52. $user= new $user();
  53. //调用方法==>已经解析到(3方法),传参==>(4参数)
  54. $user->$edit(...array_values($params));
  55. echo $user->id;
  56. echo $user->name;

path_info方式

  1. <?php
  2. class Action
  3. {
  4. public $id;
  5. public $name;
  6. public function edit($id,$name)
  7. {
  8. $this->id = $id;
  9. $this->name = $name;
  10. }
  11. }
  12. //1,过滤空数组
  13. $pathinfo=array_filter(array_filter(explode('/',$_SERVER['PATH_INFO'])));
  14. //,2,解析控制器 。初始数据
  15. $user=ucfirst(array_shift($pathinfo));
  16. //ucfirst首字母大写
  17. //3,解析方法同上
  18. $edit=array_shift($pathinfo);
  19. //,4,解析参数
  20. parse_str($_SERVER['PATH_INFO'],$params);
  21. //print_r($params);
  22. //5,调用控制器
  23. $user= new $user();
  24. //传入过滤空数组的数组
  25. $user->$edit(...array_values($pathinfo));
  26. echo $user->id;
  27. echo $user->name;

总结:调用控制器这里有比较懵。跨度太大了。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:你想一直停留在1+1吗
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