改进Think\Route类 支持url映射定义

This commit is contained in:
thinkphp
2013-03-25 16:53:53 +08:00
parent a28899656b
commit f426a438cf
2 changed files with 72 additions and 65 deletions

View File

@@ -179,7 +179,8 @@ class App {
}
}
}
// 监听path_info
Tag::listen('path_info');
// 分析PATHINFO信息
if(empty($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
$types = explode(',',$config['pathinfo_fetch']);
@@ -195,19 +196,18 @@ class App {
}
}
// 定位模块
if(!empty($_SERVER['PATH_INFO'])) {
// 监听path_info
Tag::listen('path_info');
$part = pathinfo($_SERVER['PATH_INFO']);
define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
$_SERVER['PATH_INFO'] = preg_replace('/\.('.trim($config['url_html_suffix'],'.').')$/i', '',$_SERVER['PATH_INFO']);
$paths = explode($config['pathinfo_depr'],trim($_SERVER['PATH_INFO'],'/'));
$part = pathinfo($_SERVER['PATH_INFO']);
define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
$_SERVER['PATH_INFO'] = preg_replace('/\.('.trim($config['url_html_suffix'],'.').')$/i', '',$_SERVER['PATH_INFO']);
$url = trim($_SERVER['PATH_INFO'],'/');
if($url) {
$paths = explode($config['pathinfo_depr'],$url);
if($config['require_module'] && !isset($_GET[$var_m])) {
$_GET[$var_m] = array_shift($paths);
$_SERVER['PATH_INFO'] = implode('/',$paths);
}
}elseif(isset($_GET[$var_m]) && !$config['require_module']) {
unset($_GET[$var_m]);
}
// 获取模块名称
define('MODULE_NAME',strtolower(isset($_GET[$var_m])?$_GET[$var_m]:$config['default_module']));
@@ -242,27 +242,8 @@ class App {
}else{
_404('module not exists :'.MODULE_NAME);
}
if(!empty($_SERVER['PATH_INFO'])) {
Tag::listen('path_info');
$url = trim(substr_replace($_SERVER['PATH_INFO'],'',0,strlen($_GET[$var_m])+1),'/');
// 模块路由检测
if(!$config['url_route'] || !Route::check($url)){
$paths = explode($config['pathinfo_depr'],$url);
if($config['require_controller'] && !isset($_GET[$var_c])) {
$_GET[$var_c] = array_shift($paths);
}
if(!isset($_GET[$var_a])) {
$_GET[$var_a] = array_shift($paths);
}
// 解析剩余的URL参数
$var = [];
preg_replace('@(\w+)\/([^\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/',$paths));
$_GET = array_merge($var,$_GET);
}
}elseif(isset($_GET[$var_c]) && !$config['require_controller']) {
unset($_GET[$var_c]);
}
// 路由检测
Route::check(trim($_SERVER['PATH_INFO'],'/'));
// 获取控制器名
define('CONTROLLER_NAME', strtolower(isset($_GET[$var_c])?$_GET[$var_c]:$config['default_controller']));

View File

@@ -20,6 +20,14 @@ class Route {
'all' => [],
];
// 路由规则映射
static private $map = [];
// 添加映射规则
static public function map($url,$route){
self::$map[$url] = $route;
}
// 添加某个路由规则
static public function add($rule,$route,$type='get'){
if(is_array($type)) {
@@ -66,15 +74,18 @@ class Route {
// 检测URL路由
static public function check($regx) {
// 优先检测是否存在PATH_INFO
if(empty($regx)) return true;
// 路由处理
if(empty($regx)) $regx = '/' ;
// 分隔符替换 确保路由定义使用统一的分隔符
$regx = str_replace(Config::get('pathinfo_depr'),'/',$regx);
if(isset(self::$map[$regx])) { // URL映射
return self::parseUrl(self::$map[$regx]);
}
// 路由规则检测
$rules = self::$rules[strtolower($_SERVER['REQUEST_METHOD'])];
if(!empty(self::$rules['all'])) {
$rules = array_merge(self::$rules['all'],$rules);
}
if(!empty($rules)) {
// 分隔符替换 确保路由定义使用统一的分隔符
$regx = str_replace(Config::get('pathinfo_depr'),'/',$regx);
foreach ($rules as $rule=>$route){
if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由
if($route instanceof \Closure) {
@@ -104,7 +115,49 @@ class Route {
}
}
}
return false;
return self::parseUrl($regx);
}
// 解析模块的URL地址
static private function parseUrl($url) {
if('/'==$url) {
return ;
}
$paths = explode('/',$url);
$var_c = Config::get('var_controller');
$var_a = Config::get('var_action');
if(Config::get('require_controller') && !isset($_GET[$var_c])) {
$_GET[$var_c] = array_shift($paths);
}
if(!isset($_GET[$var_a])) {
$_GET[$var_a] = array_shift($paths);
}
// 解析剩余的URL参数
$var = [];
preg_replace('@(\w+)\/([^\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/',$paths));
$_GET = array_merge($var,$_GET);
}
// 解析规范的路由地址
// 地址格式 [模块/控制器/操作?]参数1=值1&参数2=值2...
static private function parseRoute($url) {
$var = [];
if(false !== strpos($url,'?')) { // [模块/控制器/操作?]参数1=值1&参数2=值2...
$info = parse_url($url);
$path = explode('/',$info['path']);
parse_str($info['query'],$var);
}elseif(strpos($url,'/')){ // [模块/控制器/操作]
$path = explode('/',$url);
}else{ // 参数1=值1&参数2=值2...
parse_str($url,$var);
}
if(isset($path)) {
$_GET[Config::get('var_action')] = array_pop($path);
if(!empty($path)) {
$_GET[Config::get('var_controller')] = array_pop($path);
}
}
return $var;
}
// 检测URL和规则路由是否匹配
@@ -131,31 +184,6 @@ class Route {
return true;
}
// 解析规范的路由地址
// 地址格式 [模块/控制器/操作?]参数1=值1&参数2=值2...
static private function parseUrl($url) {
$var = [];
if(false !== strpos($url,'?')) { // [模块/控制器/操作?]参数1=值1&参数2=值2...
$info = parse_url($url);
$path = explode('/',$info['path']);
parse_str($info['query'],$var);
}elseif(strpos($url,'/')){ // [模块/控制器/操作]
$path = explode('/',$url);
}else{ // 参数1=值1&参数2=值2...
parse_str($url,$var);
}
if(isset($path)) {
$_GET[Config::get('var_action')] = array_pop($path);
if(!empty($path)) {
$_GET[Config::get('var_controller')] = array_pop($path);
}
if(!empty($path)) {
$_GET[Config::get('var_module')] = array_pop($path);
}
}
return $var;
}
// 解析规则路由
// '路由规则'=>'[模块/控制器/操作]?额外参数1=值1&额外参数2=值2...'
// '路由规则'=>array('[模块/控制器/操作]','额外参数1=值1&额外参数2=值2...')
@@ -196,7 +224,7 @@ class Route {
exit;
}else{
// 解析路由地址
$var = self::parseUrl($url);
$var = self::parseRoute($url);
// 解析路由地址里面的动态参数
$values = array_values($matches);
foreach ($var as $key=>$val){
@@ -216,7 +244,6 @@ class Route {
}
$_GET = array_merge($var,$_GET);
}
return true;
}
// 解析正则路由
@@ -236,7 +263,7 @@ class Route {
exit;
}else{
// 解析路由地址
$var = self::parseUrl($url);
$var = self::parseRoute($url);
// 解析剩余的URL参数
$regx = substr_replace($regx,'',0,strlen($matches[0]));
if($regx) {
@@ -249,6 +276,5 @@ class Route {
}
$_GET = array_merge($var,$_GET);
}
return true;
}
}