<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* ThinkPHP内置的Dispatcher类
* 完成URL解析、路由和调度
* @category Think
* @package Think
* @subpackage Core
* @author liu21st <liu21st@gmail.com>
*/
class Dispatcher {
/**
* URL映射到控制器
* @access public
* @return void
*/
static public function dispatch() {
$urlMode = C('URL_MODEL');//默认值为1-(PATHINFO 模式)
//C('VAR_PATHINFO')-默认情况下是有值的 ; $_GET[C('VAR_PATHINFO')])-如果没有以get传参的话,就获取不到值了
if(!empty($_GET[C('VAR_PATHINFO')])) { // 判断URL里面是否有兼容模式参数
//'VAR_PATHINFO'=> 's', // PATHINFO 兼容模式获取变量例如 ?s=/module/action/id/1 后面的参数取决于URL_PATHINFO_DEPR
$_SERVER['PATH_INFO'] = $_GET[C('VAR_PATHINFO')];
unset($_GET[C('VAR_PATHINFO')]);
}
if($urlMode == URL_COMPAT ){//兼容模式的情况
// 兼容模式判断
define('PHP_FILE',_PHP_FILE_.'?'.C('VAR_PATHINFO').'=');
}elseif($urlMode == URL_REWRITE ) {//重写模式的情况
//当前项目地址
$url = dirname(_PHP_FILE_);
if($url == '/' || $url == '\\')
$url = '';
define('PHP_FILE',$url);
}else {//默认执行
//当前项目地址,"/index.php"
define('PHP_FILE',_PHP_FILE_);
}
// 开启子域名部署
if(C('APP_SUB_DOMAIN_DEPLOY')) {//是否开启子域名部署,默认值为false
$rules = C('APP_SUB_DOMAIN_RULES');
$subDomain = strtolower(substr($_SERVER['HTTP_HOST'],0,strpos($_SERVER['HTTP_HOST'],'.')));
define('SUB_DOMAIN',$subDomain); // 二级域名定义
if($subDomain && isset($rules[$subDomain])) {
$rule = $rules[$subDomain];
}elseif(isset($rules['*'])){ // 泛域名支持
if('www' != $subDomain && !in_array($subDomain,C('APP_SUB_DOMAIN_DENY'))) {
$rule = $rules['*'];
}
}
if(!empty($rule)) {
// 子域名部署规则 '子域名'=>array('分组名/[模块名]','var1=a&var2=b');
$array = explode('/',$rule[0]);
$module = array_pop($array);
if(!empty($module)) {
$_GET[C('VAR_MODULE')] = $module;
$domainModule = true;
}
if(!empty($array)) {
$_GET[C('VAR_GROUP')] = array_pop($array);
$domainGroup = true;
}
if(isset($rule[1])) { // 传入参数
parse_str($rule[1],$parms);
$_GET = array_merge($_GET,$parms);
}
}
}
// 分析PATHINFO信息
if(empty($_SERVER['PATH_INFO'])) {//默认为空,在有兼容模式参数时才不会为空
//ORIG_PATH_INFO,REDIRECT_PATH_INFO,REDIRECT_URL
$types = explode(',',C('URL_PATHINFO_FETCH'));
foreach ($types as $type){
//strpos($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;
}
}
}
$depr = C('URL_PATHINFO_DEPR');//PATHINFO模式下,各参数之间的分割符号
if(!empty($_SERVER['PATH_INFO'])) {//默认不执行
tag('path_info');
$part = pathinfo($_SERVER['PATH_INFO']);
define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
if(C('URL_HTML_SUFFIX')) {
$_SERVER['PATH_INFO'] = preg_replace('/\.('.trim(C('URL_HTML_SUFFIX'),'.').')$/i', '', $_SERVER['PATH_INFO']);
}elseif(__EXT__) {
$_SERVER['PATH_INFO'] = preg_replace('/.'.__EXT__.'$/i','',$_SERVER['PATH_INFO']);
}
if(!self::routerCheck()){ // 检测路由规则 如果没有则按默认规则调度URL
$paths = explode($depr,trim($_SERVER['PATH_INFO'],'/'));
if(C('VAR_URL_PARAMS')) {
// 直接通过$_GET['_URL_'][1] $_GET['_URL_'][2] 获取URL参数 方便不用路由时参数获取
$_GET[C('VAR_URL_PARAMS')] = $paths;
}
$var = array();
if (C('APP_GROUP_LIST') && !isset($_GET[C('VAR_GROUP')])){
$var[C('VAR_GROUP')] = in_array(strtolower($paths[0]),explode(',',strtolower(C('APP_GROUP_LIST'))))? array_shift($paths) : '';
if(C('APP_GROUP_DENY') && in_array(strtolower($var[C('VAR_GROUP')]),explode(',',strtolower(C('APP_GROUP_DENY'))))) {
// 禁止直接访问分组
exit;
}
}
if(!isset($_GET[C('VAR_MODULE')])) {// 还没有定义模块名称
$var[C('VAR_MODULE')] = array_shift($paths);
}
$var[C('VAR_ACTION')] = array_shift($paths);
// 解析剩余的URL参数
preg_replace('@(\w+)\/([^\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/',$paths));
$_GET = array_merge($var,$_GET);
}
define('__INFO__',$_SERVER['PATH_INFO']);
}
// 获取分组 模块和操作名称
if (C('APP_GROUP_LIST')) {//项目分组设定,多个组之间用逗号分隔,例如'Home,Admin',默认为空
define('GROUP_NAME', self::getGroup(C('VAR_GROUP')));//默认的分组变量:g
}
//模块名
define('MODULE_NAME',self::getModule(C('VAR_MODULE')));//默认的模块变量:m
//操作名
define('ACTION_NAME',self::getAction(C('VAR_ACTION')));//默认的操作变量:a
// URL常量,'/index.php'
define('__SELF__',strip_tags($_SERVER['REQUEST_URI']));
// 当前项目地址,'/index.php'
define('__APP__',strip_tags(PHP_FILE));
// 当前模块和分组地址
if(defined('GROUP_NAME')) {
define('__GROUP__',(!empty($domainGroup) || strtolower(GROUP_NAME) == strtolower(C('DEFAULT_GROUP')) )?__APP__ : __APP__.'/'.GROUP_NAME);
define('__URL__',!empty($domainModule)?__GROUP__.$depr : __GROUP__.$depr.MODULE_NAME);
}else{
// "/index.php/Index"
define('__URL__',!empty($domainModule)?__APP__.'/' : __APP__.'/'.MODULE_NAME);
}
// 当前操作地址, "/index.php/Index/index"
define('__ACTION__',__URL__.$depr.ACTION_NAME);
//保证$_REQUEST正常取值
$_REQUEST = array_merge($_POST,$_GET);
}
/**
* 路由检测
* @access public
* @return void
*/
static public function routerCheck() {
$return = false;
// 路由检测标签
tag('route_check',$return);
return $return;
}
/**
* 获得实际的模块名称
* @access private
* @return string
*/
static private function getModule($var) {
$module = (!empty($_GET[$var])? $_GET[$var]:C('DEFAULT_MODULE'));//默认的模块名:Index
unset($_GET[$var]);
if(C('URL_CASE_INSENSITIVE')) {
// URL地址不区分大小写
// 智能识别方式 index.php/user_type/index/ 识别到 UserTypeAction 模块
$module = ucfirst(parse_name($module,1));
}
return strip_tags($module);//去除html标签
}
/**
* 获得实际的操作名称
* @access private
* @return string
*/
static private function getAction($var) {
$action = !empty($_POST[$var]) ?
$_POST[$var] :
(!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_ACTION'));//默认的控制器名:index
unset($_POST[$var],$_GET[$var]);
//C('URL_CASE_INSENSITIVE'):URL是否区分大小写
return strip_tags(C('URL_CASE_INSENSITIVE')?strtolower($action):$action);
}
/**
* 获得实际的分组名称
* @access private
* @return string
*/
static private function getGroup($var) {
$group = (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_GROUP'));//默认的分组名:Home
unset($_GET[$var]);
//C('URL_CASE_INSENSITIVE'):URL是否区分大小写
return strip_tags(C('URL_CASE_INSENSITIVE') ?ucfirst(strtolower($group)):$group);
}
}
【thinkphp3.x】ThinkPHP/Lib/Core/Dispatcher.class.php文件分析
最新推荐文章于 2021-09-04 18:29:34 发布