改进Request类和Route类的checkDomain方法

This commit is contained in:
thinkphp
2016-06-13 17:51:21 +08:00
parent 8c7302f04f
commit e14174fd63
2 changed files with 12 additions and 5 deletions

View File

@@ -192,6 +192,11 @@ class Request
$options['cookie'] = $cookie; $options['cookie'] = $cookie;
$options['file'] = $files; $options['file'] = $files;
$options['server'] = $server; $options['server'] = $server;
$options['url'] = $server['REQUEST_URI'];
$options['baseUrl'] = $info['path'];
$options['pathinfo'] = $info['path'];
$options['method'] = $server['REQUEST_METHOD'];
$options['domain'] = $server['HTTP_HOST'];
self::$instance = new self($options); self::$instance = new self($options);
return self::$instance; return self::$instance;
} }

View File

@@ -523,24 +523,26 @@ class Route
/** /**
* 检测子域名部署 * 检测子域名部署
* @access public * @access public
* @param \think\Request $request Request请求对象
* @return void * @return void
*/ */
public static function checkDomain() public static function checkDomain($request)
{ {
// 域名规则 // 域名规则
$rules = self::$domain; $rules = self::$domain;
// 开启子域名部署 支持二级和三级域名 // 开启子域名部署 支持二级和三级域名
if (!empty($rules)) { if (!empty($rules)) {
if (isset($rules[$_SERVER['HTTP_HOST']])) { $host = $request->host();
if (isset($rules[$host])) {
// 完整域名或者IP配置 // 完整域名或者IP配置
$rule = $rules[$_SERVER['HTTP_HOST']]; $rule = $rules[$host];
} else { } else {
$rootDomain = Config::get('url_domain_root'); $rootDomain = Config::get('url_domain_root');
if ($rootDomain) { if ($rootDomain) {
// 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置 // 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置
$domain = explode('.', rtrim(stristr($_SERVER['HTTP_HOST'], $rootDomain, true), '.')); $domain = explode('.', rtrim(stristr($host, $rootDomain, true), '.'));
} else { } else {
$domain = explode('.', $_SERVER['HTTP_HOST'], -2); $domain = explode('.', $host, -2);
} }
// 子域名配置 // 子域名配置
if (!empty($domain)) { if (!empty($domain)) {