心理咨询网
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年11月6日
  7. * 生成指定模块下控制器方法的跳转路径
  8. */
  9. namespace core\basic;
  10. class Url
  11. {
  12. // 存储已经生成过的地址信息
  13. private static $urls = array();
  14. // 接收控制器方法完整访问路径,如:/home/Index/index /模块/控制器/方法/.. 路径,生成可访问地址
  15. public static function get($path, $suffix = null)
  16. {
  17. if (strpos($path, 'http') === 0 || ! $path) {
  18. return $path;
  19. }
  20. $path = trim_slash($path); // 去除两端斜线
  21. if (! isset(self::$urls[$path])) {
  22. $path_arr = explode('/', $path); // 地址数组
  23. if ($suffix && Config::get('app_url_type') == 2 && strrpos(strtolower($_SERVER["SCRIPT_NAME"]), 'index.php') !== false) {
  24. $url_ext = Config::get('url_rule_suffix'); // 伪静态文件形式
  25. } elseif (Config::get('app_url_type') == 1 || Config::get('app_url_type') == 2) {
  26. $url_ext = '/'; // pathinfo目录形式
  27. } else {
  28. $url_ext = '';
  29. }
  30. // 路由处理
  31. if (! ! $routes = Config::get('url_route')) {
  32. foreach ($routes as $key => $value) {
  33. // 去除两端斜线
  34. $value = trim_slash($value);
  35. $key = trim_slash($key);
  36. // 替换原来正则为替换内容
  37. if (preg_match_all('/\(.*?\)/', $key, $source)) {
  38. foreach ($source[0] as $kk => $vk) {
  39. $key = str_replace($vk, '$' . ($kk + 1), $key);
  40. }
  41. }
  42. // 替换原来替换内容为正则
  43. if (preg_match_all('/\$([0-9]+)/', $value, $destination)) {
  44. foreach ($destination[1] as $kv => $vv) {
  45. $value = str_replace($destination[0][$kv], $source[0][$vv - 1], $value);
  46. }
  47. }
  48. // 执行匹配替换
  49. if (preg_match('{' . $value . '$}i', $path)) {
  50. $path = preg_replace('{' . $value . '$}i', $key, $path);
  51. } elseif (preg_match('{' . $value . '\/}i', $path)) {
  52. $path = preg_replace('{' . $value . '\/}i', $key . '/', $path);
  53. }
  54. }
  55. }
  56. // 域名绑定处理匹配
  57. $cut_str = '';
  58. if (! ! $domains = Config::get('app_domain_bind')) {
  59. foreach ($domains as $key => $value) {
  60. $value = trim_slash($value); // 去除两端斜线
  61. if (strpos($path, $value . '/') === 0) {
  62. $cut_str = $value;
  63. $server_name = get_http_host();
  64. if ($server_name != $key) { // 绑定的域名与当前域名不一致时,添加主机地址
  65. $host = is_https() ? 'https://' . $key : 'http://' . $key;
  66. } else {
  67. $host = '';
  68. }
  69. break;
  70. }
  71. }
  72. }
  73. // 入口文件绑定匹配
  74. if (defined('URL_BIND') && $path_arr[0] == M) {
  75. $cut_str = trim_slash(URL_BIND);
  76. }
  77. // 执行URL简化
  78. if ($cut_str) {
  79. $path = substr($path, strlen($cut_str) + 1);
  80. }
  81. // 保存处理过的地址
  82. if ($path) {
  83. self::$urls[$path] = $host . url_index_path() . '/' . $path . $url_ext;
  84. } else {
  85. self::$urls[$path] = $host . url_index_path(); // 获取根路径前置地址
  86. }
  87. }
  88. return self::$urls[$path];
  89. }
  90. // 生成前端地址
  91. public static function home($path, $suffix = null, $qs = null)
  92. {
  93. if (! isset(self::$urls[md5($path . $suffix . $qs)])) {
  94. $url_rule_type = Config::get('url_rule_type') ?: 3;
  95. $url_rule_suffix = Config::get('url_rule_suffix') ?: '.html';
  96. if ($suffix === true) {
  97. $suffix = $url_rule_suffix;
  98. } elseif ($suffix === false) {
  99. $suffix = '';
  100. } else {
  101. $suffix = '/';
  102. }
  103. // 去除默认模块及控制器部分
  104. $path = trim($path, '/');
  105. $path = str_replace('home/Index', '', $path);
  106. $path = trim($path, '/');
  107. if (! $path) { // 地址前缀
  108. if ($url_rule_type == 1) {
  109. $link = SITE_INDEX_DIR . '/index.php/';
  110. } elseif ($url_rule_type == 2) {
  111. $link = SITE_INDEX_DIR . '/';
  112. } else {
  113. $link = SITE_INDEX_DIR . '/?';
  114. }
  115. } else {
  116. switch ($url_rule_type) {
  117. case '1': // 普通模式
  118. $qs = $qs ? "?" . $qs : '';
  119. $link = SITE_INDEX_DIR . '/index.php' . '/' . $path . $suffix . $qs;
  120. break;
  121. case '2': // 伪静态模式
  122. $qs = $qs ? "?" . $qs : '';
  123. $link = SITE_INDEX_DIR . '/' . $path . $suffix . $qs;
  124. break;
  125. case '3': // 兼容模式
  126. $qs = $qs ? "&" . $qs : '';
  127. $link = SITE_INDEX_DIR . '/?' . $path . $suffix . $qs;
  128. break;
  129. default:
  130. error('地址模式设置错误,请登录后台重新设置!');
  131. }
  132. }
  133. //优化分站超链接 @cms88
  134. if( ISURL!==TRUE && strpos($link,'http') == false && cookie('city')!=='' && cookie('city')!==null ){
  135. if( $path == '' && $suffix === $url_rule_suffix ){
  136. $link = $suffix;
  137. }
  138. $link = '/'.cookie('city').$link;
  139. }
  140. self::$urls[md5($path . $suffix . $qs)] = $link;
  141. }
  142. return self::$urls[md5($path . $suffix . $qs)];
  143. }
  144. }