改进Url类 域名部署url生成 URL生成不依赖 url_domain_deploy 配置参数

This commit is contained in:
thinkphp
2016-10-26 11:09:49 +08:00
parent 31aae17f55
commit 39cbbc0a38
2 changed files with 42 additions and 27 deletions

View File

@@ -277,7 +277,7 @@ if (!function_exists('url')) {
* @param bool|string $domain 域名 * @param bool|string $domain 域名
* @return string * @return string
*/ */
function url($url = '', $vars = '', $suffix = true, $domain = false) function url($url = '', $vars = '', $suffix = true, $domain = true)
{ {
return Url::build($url, $vars, $suffix, $domain); return Url::build($url, $vars, $suffix, $domain);
} }

View File

@@ -20,6 +20,7 @@ class Url
{ {
// 生成URL地址的root // 生成URL地址的root
protected static $root; protected static $root;
protected static $bindCheck;
/** /**
* URL生成 支持路由反射 * URL生成 支持路由反射
@@ -29,11 +30,8 @@ class Url
* @param boolean|string $domain 是否显示域名 或者直接传入域名 * @param boolean|string $domain 是否显示域名 或者直接传入域名
* @return string * @return string
*/ */
public static function build($url = '', $vars = '', $suffix = true, $domain = null) public static function build($url = '', $vars = '', $suffix = true, $domain = true)
{ {
if (false === $domain && Config::get('url_domain_deploy')) {
$domain = true;
}
// 解析URL // 解析URL
if (0 === strpos($url, '[') && $pos = strpos($url, ']')) { if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
// [name] 表示使用路由命名标识生成URL // [name] 表示使用路由命名标识生成URL
@@ -112,11 +110,13 @@ class Url
} }
// 检测URL绑定 // 检测URL绑定
$type = Route::getBind('type'); if (!self::$bindCheck) {
if ($type) { $type = Route::getBind('type');
$bind = Route::getBind($type); if ($type) {
if (0 === strpos($url, $bind)) { $bind = Route::getBind($type);
$url = substr($url, strlen($bind) + 1); if (0 === strpos($url, $bind)) {
$url = substr($url, strlen($bind) + 1);
}
} }
} }
// 还原URL分隔符 // 还原URL分隔符
@@ -173,15 +173,28 @@ class Url
// 解析到 模块/控制器/操作 // 解析到 模块/控制器/操作
$module = $request->module(); $module = $request->module();
if (true === $domain && 2 == substr_count($url, '/')) { if (true === $domain && 2 == substr_count($url, '/')) {
$current = $request->host();
$domains = Route::rules('domain'); $domains = Route::rules('domain');
$match = [];
$pos = [];
foreach ($domains as $key => $item) { foreach ($domains as $key => $item) {
if (isset($item['[bind]']) && 0 === strpos($url, $item['[bind]'][0])) { if (isset($item['[bind]']) && 0 === strpos($url, $item['[bind]'][0])) {
$url = substr($url, strlen($item['[bind]'][0]) + 1); $pos[$key] = strlen($item['[bind]'][0]) + 1;
$domain = $key; $match[] = $key;
$module = ''; $module = '';
break;
} }
} }
if ($match) {
$domain = current($match);
foreach ($match as $item) {
if (0 === strpos($current, $item)) {
$domain = $item;
}
}
self::$bindCheck = true;
$url = substr($url, $pos[$domain]);
}
} elseif ($domain) { } elseif ($domain) {
if (isset($domains[$domain]['[bind]'][0])) { if (isset($domains[$domain]['[bind]'][0])) {
$bindModule = $domains[$domain]['[bind]'][0]; $bindModule = $domains[$domain]['[bind]'][0];
@@ -213,15 +226,15 @@ class Url
if (!$domain) { if (!$domain) {
return ''; return '';
} }
$request = Request::instance(); $request = Request::instance();
$rootDomain = Config::get('url_domain_root');
if (true === $domain) { if (true === $domain) {
// 自动判断域名 // 自动判断域名
$domain = $request->host(); $domain = $request->host();
if (Config::get('url_domain_deploy')) {
// 根域名 $domains = Route::rules('domain');
$urlDomainRoot = Config::get('url_domain_root'); if ($domains) {
$domains = Route::rules('domain'); $route_domain = array_keys($domains);
$route_domain = array_keys($domains);
foreach ($route_domain as $domain_prefix) { foreach ($route_domain as $domain_prefix) {
if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== false) { if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== false) {
foreach ($domains as $key => $rule) { foreach ($domains as $key => $rule) {
@@ -230,13 +243,13 @@ class Url
$url = ltrim($url, $rule); $url = ltrim($url, $rule);
$domain = $key; $domain = $key;
// 生成对应子域名 // 生成对应子域名
if (!empty($urlDomainRoot)) { if (!empty($rootDomain)) {
$domain .= $urlDomainRoot; $domain .= $rootDomain;
} }
break; break;
} else if (false !== strpos($key, '*')) { } else if (false !== strpos($key, '*')) {
if (!empty($urlDomainRoot)) { if (!empty($rootDomain)) {
$domain .= $urlDomainRoot; $domain .= $rootDomain;
} }
break; break;
} }
@@ -244,13 +257,15 @@ class Url
} }
} }
} }
} elseif (!strpos($domain, '.')) {
$rootDomain = Config::get('url_domain_root'); } else {
if (empty($rootDomain)) { if (empty($rootDomain)) {
$host = $request->host(); $host = $request->host();
$rootDomain = substr_count($host, '.') > 1 ? substr(strstr($host, '.'), 1) : $host; $rootDomain = substr_count($host, '.') > 1 ? substr(strstr($host, '.'), 1) : $host;
} }
$domain .= '.' . $rootDomain; if (!strpos($domain, $rootDomain)) {
$domain .= '.' . $rootDomain;
}
} }
return ($request->isSsl() ? 'https://' : 'http://') . $domain; return ($request->isSsl() ? 'https://' : 'http://') . $domain;
} }