fork download
  1. <?php
  2.  
  3. class router {
  4.  
  5. private $_uri = [];
  6. private $_query = [];
  7.  
  8. public function add($uri, $query) {
  9. $this->_uri[] = $uri;
  10. $this->_query[] = $query;
  11. }
  12.  
  13. public function submit() {
  14. $url = $this->explode_url();
  15. foreach ($this->_uri as $key => $value) {
  16. if (!empty($url[0])) {
  17. if ($value['controller'] == ':variable') {
  18. $getting_url['controller'] = $url[0];
  19. goto a;
  20. } elseif ($value['controller'] == $url[0]) {
  21. // controller matched!
  22. if (empty($url[1])) {
  23. call_user_func($this->_query[$key]);
  24. // finish
  25. } else {
  26. a:
  27. if ($value['function'] == ':variable') {
  28. $getting_url['function'] = $url[1];
  29. goto b;
  30. } elseif ($url[1] == $value['function']) {
  31. if (empty($url[2])) {
  32. call_user_func($this->_query[$key]);
  33. // finish
  34. } else {
  35. b:
  36. if ($url[2] == $value['data']) {
  37. call_user_func($this->_query[$key]);
  38. } elseif ($value['data'] == ':variable') {
  39. $getting_url['data'] = $url[2];
  40. }
  41. }
  42. } else {
  43. echo 'no function';
  44. }
  45. }
  46. } else {
  47. echo 'no';
  48. }
  49. }
  50. }
  51.  
  52. print_r($getting_url);
  53. }
  54.  
  55. private function explode_url() {
  56. $url = explode('/', trim($_GET['url'], '/'));
  57. $data = [];
  58. $str = '';
  59. foreach ($url as $key => $value) {
  60. if ($key >= 2) {
  61. $str = $str . '/' . $value;
  62. } else {
  63. $data[] = $value;
  64. }
  65. }
  66.  
  67. $data[] = trim($str, '/');
  68. return $data;
  69. }
  70.  
  71. }
  72.  
  73. ?>
  74.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty