Url.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. class LtUrl
  3. {
  4. public $configHandle;
  5. public $routingTable;
  6. public $baseUrl;
  7. public function __construct()
  8. {
  9. if (! $this->configHandle instanceof LtConfig)
  10. {
  11. if (class_exists("LtObjectUtil", false))
  12. {
  13. $this->configHandle = LtObjectUtil::singleton("LtConfig");
  14. }
  15. else
  16. {
  17. $this->configHandle = new LtConfig;
  18. }
  19. }
  20. }
  21. public function init()
  22. {
  23. $this->routingTable = $this->configHandle->get("router.routing_table");
  24. if (empty($this->routingTable))
  25. {
  26. $this->routingTable = array('pattern' => ":module/:action/*",
  27. 'default' => array('module' => 'default', 'action' => 'index'),
  28. 'reqs' => array('module' => '[a-zA-Z0-9\.\-_]+',
  29. 'action' => '[a-zA-Z0-9\.\-_]+'
  30. ),
  31. 'varprefix' => ':',
  32. 'delimiter' => '/',
  33. 'postfix' => '',
  34. 'protocol' => 'PATH_INFO', // REWRITE STANDARD
  35. );
  36. }
  37. $protocol = strtoupper($this->routingTable['protocol']);
  38. if ('REWRITE' == $protocol)
  39. {
  40. $this->baseUrl = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME) . '/';
  41. }
  42. else if ('STANDARD' == $protocol)
  43. {
  44. $this->baseUrl = $_SERVER['PHP_SELF'];
  45. }
  46. else
  47. {
  48. $this->baseUrl = '';
  49. }
  50. }
  51. public function generate($module, $action, $args = array())
  52. {
  53. $args = array_merge(array('module' => $module, 'action' => $action), $args);
  54. $url = '';
  55. // $url = $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  56. // $url .= $_SERVER['HTTP_HOST'];
  57. // $url .= $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
  58. $url .= $this->baseUrl;
  59. $url .= $this->reverseMatchingRoutingTable($args);
  60. return $url;
  61. }
  62. /**
  63. * 将变量反向匹配路由表, 返回匹配后的url
  64. *
  65. * @param array $params
  66. * @return string
  67. */
  68. public function reverseMatchingRoutingTable($args)
  69. {
  70. $ret = $this->routingTable['pattern'];
  71. $default = $this->routingTable['default'];
  72. $reqs = $this->routingTable['reqs'];
  73. $delimiter = $this->routingTable['delimiter'];
  74. $varprefix = $this->routingTable['varprefix'];
  75. $postfix = $this->routingTable['postfix'];
  76. $protocol = strtoupper($this->routingTable['protocol']);
  77. if ('STANDARD' == $protocol)
  78. {
  79. return '?' . http_build_query($args, '', '&');
  80. }
  81. $pattern = explode($delimiter, trim($this->routingTable['pattern'], $delimiter));
  82. foreach($pattern as $k => $v)
  83. {
  84. if ($v[0] == $varprefix)
  85. {
  86. // 变量
  87. $varname = substr($v, 1);
  88. // 匹配变量
  89. if (isset($args[$varname]))
  90. {
  91. $regex = "/^{$reqs[$varname]}\$/i";
  92. if (preg_match($regex, $args[$varname]))
  93. {
  94. $ret = str_replace($v, $args[$varname], $ret);
  95. unset($args[$varname]);
  96. }
  97. }
  98. else if (isset($default[$varname]))
  99. {
  100. $ret = str_replace($v, $default[$varname], $ret);
  101. }
  102. }
  103. else if ($v[0] == '*')
  104. {
  105. // 通配符
  106. $tmp = '';
  107. foreach($args as $key => $value)
  108. {
  109. if (!isset($default[$key]))
  110. {
  111. $tmp .= $key . $delimiter . rawurlencode($value) . $delimiter;
  112. }
  113. }
  114. $tmp = rtrim($tmp, $delimiter);
  115. $ret = str_replace($v, $tmp, $ret);
  116. $ret = rtrim($ret, $delimiter);
  117. }
  118. else
  119. {
  120. // 静态
  121. }
  122. }
  123. if ('REWRITE' == $protocol)
  124. {
  125. $ret = $ret . $postfix;
  126. }
  127. else if ('PATH_INFO' == $protocol)
  128. {
  129. $ret = $_SERVER['SCRIPT_NAME'] . $delimiter . $ret . $postfix;
  130. }
  131. else
  132. {
  133. $ret = $ret . $postfix;
  134. }
  135. return $ret;
  136. }
  137. }