改进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 域名
* @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);
}

View File

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