think\Route::checkDomain PHP Method

checkDomain() public static method

检测子域名部署
public static checkDomain ( think\Request $request, array &$currentRules, string $method = 'GET' ) : void
$request think\Request Request请求对象
$currentRules array 当前路由规则
$method string 请求类型
return void
    public static function checkDomain($request, &$currentRules, $method = 'GET')
    {
        // 域名规则
        $rules = self::$rules['domain'];
        // 开启子域名部署 支持二级和三级域名
        if (!empty($rules)) {
            $host = $request->host();
            if (isset($rules[$host])) {
                // 完整域名或者IP配置
                $item = $rules[$host];
            } else {
                $rootDomain = Config::get('url_domain_root');
                if ($rootDomain) {
                    // 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置
                    $domain = explode('.', rtrim(stristr($host, $rootDomain, true), '.'));
                } else {
                    $domain = explode('.', $host, -2);
                }
                // 子域名配置
                if (!empty($domain)) {
                    // 当前子域名
                    $subDomain = implode('.', $domain);
                    self::$subDomain = $subDomain;
                    $domain2 = array_pop($domain);
                    if ($domain) {
                        // 存在三级域名
                        $domain3 = array_pop($domain);
                    }
                    if ($subDomain && isset($rules[$subDomain])) {
                        // 子域名配置
                        $item = $rules[$subDomain];
                    } elseif (isset($rules['*.' . $domain2]) && !empty($domain3)) {
                        // 泛三级域名
                        $item = $rules['*.' . $domain2];
                        $panDomain = $domain3;
                    } elseif (isset($rules['*']) && !empty($domain2)) {
                        // 泛二级域名
                        if ('www' != $domain2) {
                            $item = $rules['*'];
                            $panDomain = $domain2;
                        }
                    }
                }
            }
            if (!empty($item)) {
                if (isset($item['[bind]'])) {
                    // 解析子域名部署规则
                    list($rule, $option, $pattern) = $item['[bind]'];
                    if (!empty($option['https']) && !$request->isSsl()) {
                        // https检测
                        throw new HttpException(404, 'must use https request:' . $host);
                    }
                    if (strpos($rule, '?')) {
                        // 传入其它参数
                        $array = parse_url($rule);
                        $result = $array['path'];
                        parse_str($array['query'], $params);
                        if (isset($panDomain)) {
                            $pos = array_search('*', $params);
                            if (false !== $pos) {
                                // 泛域名作为参数
                                $params[$pos] = $panDomain;
                            }
                        }
                        $_GET = array_merge($_GET, $params);
                    } else {
                        $result = $rule;
                    }
                    if (0 === strpos($result, '\\')) {
                        // 绑定到命名空间 例如 \app\index\behavior
                        self::$bind = ['type' => 'namespace', 'namespace' => $result];
                    } elseif (0 === strpos($result, '@')) {
                        // 绑定到类 例如 @app\index\controller\User
                        self::$bind = ['type' => 'class', 'class' => substr($result, 1)];
                    } else {
                        // 绑定到模块/控制器 例如 index/user
                        self::$bind = ['type' => 'module', 'module' => $result];
                    }
                    self::$domainBind = true;
                } else {
                    self::$domainRule = $item;
                    $currentRules = isset($item[$method]) ? $item[$method] : $item['*'];
                }
            }
        }
    }

Usage Example

Example #1
0
 /**
  * URL调度
  * @access public
  * @return void
  */
 public static function dispatch($config)
 {
     if (isset($_GET[$config['var_pathinfo']])) {
         // 判断URL里面是否有兼容模式参数
         $_SERVER['PATH_INFO'] = $_GET[$config['var_pathinfo']];
         unset($_GET[$config['var_pathinfo']]);
     } elseif (IS_CLI) {
         // CLI模式下 index.php module/controller/action/params/...
         $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
     }
     // 检测域名部署
     if (!IS_CLI && isset($config['sub_domain_deploy']) && $config['sub_domain_deploy']) {
         Route::checkDomain();
     }
     // 监听path_info
     Hook::listen('path_info');
     // 分析PATHINFO信息
     if (!isset($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
         $types = explode(',', $config['pathinfo_fetch']);
         foreach ($types as $type) {
             if (0 === strpos($type, ':')) {
                 // 支持函数判断
                 $_SERVER['PATH_INFO'] = call_user_func(substr($type, 1));
                 break;
             } elseif (!empty($_SERVER[$type])) {
                 $_SERVER['PATH_INFO'] = 0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME']) ? substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
                 break;
             }
         }
     }
     if (empty($_SERVER['PATH_INFO'])) {
         $_SERVER['PATH_INFO'] = '';
         define('__INFO__', '');
         define('__EXT__', '');
     } else {
         $_SERVER['PATH_INFO'] = trim($_SERVER['PATH_INFO'], '/');
         define('__INFO__', $_SERVER['PATH_INFO']);
         // URL后缀
         define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION)));
         $_SERVER['PATH_INFO'] = __INFO__;
         if (__INFO__ && !defined('BIND_MODULE')) {
             if ($config['url_deny_suffix'] && preg_match('/\\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) {
                 exit;
             }
             $paths = explode($config['pathinfo_depr'], __INFO__, 2);
             // 获取URL中的模块名
             if ($config['require_module'] && !isset($_GET[VAR_MODULE])) {
                 $_GET[VAR_MODULE] = array_shift($paths);
                 $_SERVER['PATH_INFO'] = implode('/', $paths);
             }
         }
         // 去除URL后缀
         $_SERVER['PATH_INFO'] = preg_replace($config['url_html_suffix'] ? '/\\.(' . trim($config['url_html_suffix'], '.') . ')$/i' : '/\\.' . __EXT__ . '$/i', '', $_SERVER['PATH_INFO']);
     }
     // URL常量
     define('__SELF__', strip_tags($_SERVER[$config['url_request_uri']]));
     // 获取模块名称
     define('MODULE_NAME', defined('BIND_MODULE') ? BIND_MODULE : self::getModule($config));
     // 模块初始化
     if (MODULE_NAME && $config['common_module'] != MODULE_NAME && is_dir(APP_PATH . MODULE_NAME)) {
         Hook::listen('app_begin');
         define('MODULE_PATH', APP_PATH . MODULE_NAME . '/');
         define('VIEW_PATH', MODULE_PATH . VIEW_LAYER . '/');
         // 初始化模块
         self::initModule(MODULE_PATH, $config);
     } else {
         throw new Exception('module not exists :' . MODULE_NAME);
     }
     // 路由检测和控制器、操作解析
     Route::check($_SERVER['PATH_INFO'], $config['pathinfo_depr']);
     // 获取控制器名
     define('CONTROLLER_NAME', strip_tags(strtolower(isset($_GET[VAR_CONTROLLER]) ? $_GET[VAR_CONTROLLER] : $config['default_controller'])));
     // 获取操作名
     define('ACTION_NAME', strip_tags(strtolower(isset($_GET[VAR_ACTION]) ? $_GET[VAR_ACTION] : $config['default_action'])));
     unset($_GET[VAR_ACTION], $_GET[VAR_CONTROLLER], $_GET[VAR_MODULE]);
     //保证$_REQUEST正常取值
     $_REQUEST = array_merge($_POST, $_GET, $_COOKIE);
 }
All Usage Examples Of think\Route::checkDomain