diff --git a/library/think/app.php b/library/think/app.php index 6102e525..01994511 100644 --- a/library/think/app.php +++ b/library/think/app.php @@ -15,6 +15,7 @@ namespace think; * App 应用管理 * @author liu21st */ + class App { @@ -239,6 +240,7 @@ class App } } + $result = []; if (empty($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; define('__INFO__', ''); @@ -257,22 +259,33 @@ class App if (!empty($config['url_route_on'])) { // 开启路由 则检测路由配置 并默认读取 url_route_rules 参数 Route::register($config['url_route_rules']); - Route::check($_SERVER['PATH_INFO'], $config['pathinfo_depr']); + $result = Route::check(__INFO__, $config['pathinfo_depr']); + if (false === $result) { + throw new Exception('route not define '); + } + } else { + $result = Route::parseUrl(__INFO__); } - // 获取URL中的模块名 - if ($config['require_module'] && !isset($_GET[VAR_MODULE])) { - $paths = explode($config['pathinfo_depr'], __INFO__, 2); - $_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']); } + $module = strtolower($result[0] ? $result[0] : $config['default_module']); + if ($maps = $config['url_module_map']) { + if (isset($maps[$module])) { + // 记录当前别名 + define('MODULE_ALIAS', $module); + // 获取实际的项目名 + $module = $maps[MODULE_ALIAS]; + } elseif (array_search($module, $maps)) { + // 禁止访问原始项目 + $module = ''; + } + } // 获取模块名称 - define('MODULE_NAME', defined('BIND_MODULE') ? BIND_MODULE : self::getModule($config)); + define('MODULE_NAME', defined('BIND_MODULE') ? BIND_MODULE : strip_tags($module)); // 模块初始化 if (MODULE_NAME && MODULE_NAME != $config['common_module'] && is_dir(APP_PATH . MODULE_NAME)) { @@ -287,14 +300,11 @@ class App } // 获取控制器名 - define('CONTROLLER_NAME', strip_tags(strtolower(isset($_GET[VAR_CONTROLLER]) ? $_GET[VAR_CONTROLLER] : $config['default_controller']))); + define('CONTROLLER_NAME', strip_tags(strtolower($result[1] ? $result[1] : $config['default_controller']))); // 获取操作名 - define('ACTION_NAME', strip_tags(strtolower(isset($_GET[VAR_ACTION]) ? $_GET[VAR_ACTION] : $config['default_action']))); + define('ACTION_NAME', strip_tags(strtolower($result[2] ? $result[2] : $config['default_action']))); - unset($_GET[VAR_ACTION], $_GET[VAR_CONTROLLER], $_GET[VAR_MODULE]); - //保证$_REQUEST正常取值 - $_REQUEST = array_merge($_POST, $_GET, $_COOKIE); } /** diff --git a/library/think/response.php b/library/think/response.php index 9241a8f1..c369be5e 100644 --- a/library/think/response.php +++ b/library/think/response.php @@ -11,6 +11,9 @@ namespace think; +use think\Config as Config; +use think\Transform as Transform; +use think\Url as Url; class Response { @@ -85,6 +88,18 @@ class Response header('Location: ' . $url); } + /** + * 设置响应头 + * @access protected + * @param string $name 参数名 + * @param string $value 参数值 + * @return void + */ + public static function header($name, $value) + { + header($name . ':' . $value); + } + // 发送Http状态信息 public static function sendHttpStatus($status) { diff --git a/library/think/route.php b/library/think/route.php index 9fc152a9..22cbbdeb 100644 --- a/library/think/route.php +++ b/library/think/route.php @@ -155,7 +155,8 @@ class Route exit; } if (is_array($rule)) { - $_GET[VAR_MODULE] = $rule[0]; + define('BIND_MODULE', $rule[0]); + //$_GET[VAR_MODULE] = $rule[0]; if (isset($rule[1])) { // 传入参数 parse_str($rule[1], $parms); @@ -169,7 +170,8 @@ class Route $_GET = array_merge($_GET, $parms); } } else { - $_GET[VAR_MODULE] = $rule; + define('BIND_MODULE', $rule); + //$_GET[VAR_MODULE] = $rule; } } } @@ -262,7 +264,7 @@ class Route } } - return self::parseUrl($regx); + return false; } /** @@ -360,24 +362,18 @@ class Route } // 解析模块的URL地址 [模块/]控制器/操作 - private static function parseUrl($url) + public static function parseUrl($url) { if ('/' == $url) { return; } $paths = explode('/', $url); - if (!defined('BIND_MODULE') && !isset($_GET[VAR_MODULE])) { - $_GET[VAR_MODULE] = array_shift($paths); - } + $module = defined('BIND_MODULE') ? BIND_MODULE : array_shift($paths); - if (!defined('BIND_CONTROLLER') && !isset($_GET[VAR_CONTROLLER])) { - $_GET[VAR_CONTROLLER] = array_shift($paths); - } + $controller = defined('BIND_CONTROLLER') ? BIND_CONTROLLER : array_shift($paths); - if ($paths) { - $_GET[VAR_ACTION] = array_shift($paths); - } + $action = $paths ? array_shift($paths) : null; // 解析剩余的URL参数 $var = []; @@ -385,6 +381,7 @@ class Route preg_replace_callback('/(\w+)\/([^\/]+)/', function ($match) use (&$var) {$var[strtolower($match[1])] = strip_tags($match[2]);}, implode('/', $paths)); } $_GET = array_merge($var, $_GET); + return [$module, $controller, $action]; } // 解析规范的路由地址 @@ -405,16 +402,12 @@ class Route parse_str($url, $var); } if (isset($path)) { - $action = array_pop($path); - $_GET[VAR_ACTION] = '[rest]' == $action ? REQUEST_METHOD : $action; - if (!empty($path)) { - $_GET[VAR_CONTROLLER] = array_pop($path); - } - if (!empty($path)) { - $_GET[VAR_MODULE] = array_pop($path); - } + $action = array_pop($path); + $action = '[rest]' == $action ? REQUEST_METHOD : $action; + $controller = !empty($path) ? array_pop($path) : null; + $module = !empty($path) ? array_pop($path) : null; } - return $var; + return ['route' => [$module, $controller, $action], 'var' => $var]; } // 检测URL和规则路由是否匹配 @@ -495,7 +488,8 @@ class Route exit; } else { // 解析路由地址 - $var = self::parseRoute($url); + $result = self::parseRoute($url); + $var = $result['var']; // 解析路由地址里面的动态参数 $values = array_values($matches); foreach ($var as $key => $val) { @@ -516,6 +510,7 @@ class Route $var = array_merge($var, $params); } $_GET = array_merge($var, $_GET); + return $result['route']; } } @@ -538,7 +533,8 @@ class Route exit; } else { // 解析路由地址 - $var = self::parseRoute($url); + $result = self::parseRoute($url); + $var = $result['var']; // 解析剩余的URL参数 $regx = substr_replace($regx, '', 0, strlen($matches[0])); if ($regx) { @@ -552,6 +548,7 @@ class Route $var = array_merge($var, $params); } $_GET = array_merge($var, $_GET); + return $result['route']; } } }