You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

159 lines
5.0 KiB

  1. <?php
  2. namespace Kuxin\Helper;
  3. use Kuxin\Config;
  4. use Kuxin\Response;
  5. use Kuxin\Router;
  6. /**
  7. * Class Url
  8. * url辅助函数
  9. *
  10. * @package Kuxin\Helper
  11. * @author Pakey <pakey@qq.com>
  12. */
  13. class Url
  14. {
  15. /**
  16. * 获取微信用的当前URL 去掉#后面的内容
  17. *
  18. * @return string
  19. */
  20. public static function weixin()
  21. {
  22. $url = self::current();
  23. if (strpos($url, '#')) {
  24. $url = explode('#', $url)['0'];
  25. }
  26. return $url;
  27. }
  28. /**
  29. * 获取当前地址
  30. *
  31. * @return string
  32. */
  33. public static function current()
  34. {
  35. if (PHP_SAPI == 'cli') {
  36. return 'cli';
  37. }
  38. if (strpos($_SERVER['REQUEST_URI'], 'http://') === 0) {
  39. return $_SERVER['REQUEST_URI'];
  40. }
  41. $protocol = (!empty($_SERVER['HTTPS'])
  42. && $_SERVER['HTTPS'] !== 'off'
  43. || $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
  44. $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
  45. $uri = isset($_SERVER['HTTP_X_REAL_URI']) ? $_SERVER['HTTP_X_REAL_URI'] : $_SERVER['REQUEST_URI'];
  46. return $protocol . $host . $uri;
  47. }
  48. /**
  49. * 生成url
  50. *
  51. * @param string $method
  52. * @param array $args
  53. * @param string $type
  54. * @param array $ignores
  55. * @return mixed|string
  56. */
  57. public static function build($method = '', $args = [], $type = 'html', $ignores = [])
  58. {
  59. if (!empty($_REQUEST['template'])){
  60. $args['template'] = $_REQUEST['template'];
  61. }
  62. static $rules = null, $_method = [], $power = false, $default_data = [], $ignore_params = [], $auto_calc = [];
  63. if ($rules === null) {
  64. $rules = Config::get('rewrite.rules');
  65. $power = Config::get('rewrite.power', false);
  66. $default_data = Config::get('url.default_data', []);
  67. $ignore_params = Config::get('url.ignore_param', []);
  68. $auto_calc = Config::get('url.auto_calc', []);
  69. }
  70. $ignores = array_merge($ignores, $ignore_params);
  71. foreach ($args as $oarg_k => $oarg_v) {
  72. if ((isset($default_data[$oarg_k]) && $default_data[$oarg_k] == $oarg_v)) {
  73. unset($args[$oarg_k]);
  74. }
  75. }
  76. if (empty($_method[$method])) {
  77. if ($method === '') {
  78. $_method[$method] = strtolower(str_replace('\\', '.', Router::$controller) . '.' . Router::$action);
  79. } elseif (substr_count($method, '.') == 0) {
  80. $_method[$method] = strtolower(str_replace('\\', '.', Router::$controller) . '.' . $method);
  81. } else {
  82. $_method[$method] = strtolower($method);
  83. }
  84. }
  85. $method = $_method[$method];
  86. if ($power && isset($rules[$method])) {
  87. foreach ($auto_calc as $key => $var) {
  88. if (isset($args[$key])) {
  89. foreach ($var as $item) {
  90. $args[$item['name']] = $item['func']($args[$key]);
  91. }
  92. }
  93. }
  94. $keys = [];
  95. $rule = $rules[$method];
  96. $oargs = $args;
  97. foreach ($args as $key => &$arg) {
  98. $keys[] = '{' . $key . '}';
  99. $arg = rawurlencode(urldecode($arg));
  100. if (strpos($rule, '{' . $key . '}')) {
  101. unset($oargs[$key]);
  102. }
  103. }
  104. $url = self::clearUrl(str_replace($keys, $args, $rule));
  105. if (strpos($url, ']')) {
  106. $url = strtr($url, ['[' => '', ']' => '']);
  107. }
  108. if (strpos($url, '{')) {
  109. foreach ($default_data as $default_k => $default_v) {
  110. if (strpos($url, '{' . $default_k . '}')) {
  111. $url = str_replace('{' . $default_k . '}', $default_v, $url);
  112. }
  113. }
  114. }
  115. if (!empty($oargs) && $ignores) {
  116. foreach ($oargs as $oarg_k => $oarg_v) {
  117. if (in_array($oarg_k, $ignores)) {
  118. unset($oargs[$oarg_k]);
  119. }
  120. }
  121. }
  122. $url = (substr($url, 0, 1) == '/' ? '' : '/') . $url;
  123. if (empty($oargs)) {
  124. return $url;
  125. } else {
  126. return $url . (strpos($url, '?') ? '&' : '?') . http_build_query($oargs);
  127. }
  128. } else {
  129. $type = $type ? $type : Response::getType();
  130. $url = '/' . strtr($method, '.', '/') . '.' . $type;
  131. if ($args) {
  132. $url .= '?' . http_build_query($args);
  133. }
  134. return $url;
  135. }
  136. }
  137. /**
  138. * 清除url中可选参数
  139. *
  140. * @param $url
  141. * @return mixed
  142. */
  143. private static function clearUrl($url)
  144. {
  145. while (preg_match('#\[[^\[\]]*?\{\w+\}[^\[\]]*?\]#', $url, $match)) {
  146. $url = str_replace($match['0'], '', $url);
  147. }
  148. return $url;
  149. }
  150. }