mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
改进Think\Route类 支持url映射定义
This commit is contained in:
@@ -179,7 +179,8 @@ class App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 监听path_info
|
||||||
|
Tag::listen('path_info');
|
||||||
// 分析PATHINFO信息
|
// 分析PATHINFO信息
|
||||||
if(empty($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
|
if(empty($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
|
||||||
$types = explode(',',$config['pathinfo_fetch']);
|
$types = explode(',',$config['pathinfo_fetch']);
|
||||||
@@ -195,19 +196,18 @@ class App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 定位模块
|
// 定位模块
|
||||||
if(!empty($_SERVER['PATH_INFO'])) {
|
$part = pathinfo($_SERVER['PATH_INFO']);
|
||||||
// 监听path_info
|
define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
|
||||||
Tag::listen('path_info');
|
$_SERVER['PATH_INFO'] = preg_replace('/\.('.trim($config['url_html_suffix'],'.').')$/i', '',$_SERVER['PATH_INFO']);
|
||||||
$part = pathinfo($_SERVER['PATH_INFO']);
|
$url = trim($_SERVER['PATH_INFO'],'/');
|
||||||
define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
|
if($url) {
|
||||||
$_SERVER['PATH_INFO'] = preg_replace('/\.('.trim($config['url_html_suffix'],'.').')$/i', '',$_SERVER['PATH_INFO']);
|
$paths = explode($config['pathinfo_depr'],$url);
|
||||||
$paths = explode($config['pathinfo_depr'],trim($_SERVER['PATH_INFO'],'/'));
|
|
||||||
if($config['require_module'] && !isset($_GET[$var_m])) {
|
if($config['require_module'] && !isset($_GET[$var_m])) {
|
||||||
$_GET[$var_m] = array_shift($paths);
|
$_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']));
|
define('MODULE_NAME',strtolower(isset($_GET[$var_m])?$_GET[$var_m]:$config['default_module']));
|
||||||
|
|
||||||
@@ -242,27 +242,8 @@ class App {
|
|||||||
}else{
|
}else{
|
||||||
_404('module not exists :'.MODULE_NAME);
|
_404('module not exists :'.MODULE_NAME);
|
||||||
}
|
}
|
||||||
|
// 路由检测
|
||||||
if(!empty($_SERVER['PATH_INFO'])) {
|
Route::check(trim($_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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取控制器名
|
// 获取控制器名
|
||||||
define('CONTROLLER_NAME', strtolower(isset($_GET[$var_c])?$_GET[$var_c]:$config['default_controller']));
|
define('CONTROLLER_NAME', strtolower(isset($_GET[$var_c])?$_GET[$var_c]:$config['default_controller']));
|
||||||
|
|||||||
@@ -20,6 +20,14 @@ class Route {
|
|||||||
'all' => [],
|
'all' => [],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 路由规则映射
|
||||||
|
static private $map = [];
|
||||||
|
|
||||||
|
// 添加映射规则
|
||||||
|
static public function map($url,$route){
|
||||||
|
self::$map[$url] = $route;
|
||||||
|
}
|
||||||
|
|
||||||
// 添加某个路由规则
|
// 添加某个路由规则
|
||||||
static public function add($rule,$route,$type='get'){
|
static public function add($rule,$route,$type='get'){
|
||||||
if(is_array($type)) {
|
if(is_array($type)) {
|
||||||
@@ -66,15 +74,18 @@ class Route {
|
|||||||
// 检测URL路由
|
// 检测URL路由
|
||||||
static public function check($regx) {
|
static public function check($regx) {
|
||||||
// 优先检测是否存在PATH_INFO
|
// 优先检测是否存在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'])];
|
$rules = self::$rules[strtolower($_SERVER['REQUEST_METHOD'])];
|
||||||
if(!empty(self::$rules['all'])) {
|
if(!empty(self::$rules['all'])) {
|
||||||
$rules = array_merge(self::$rules['all'],$rules);
|
$rules = array_merge(self::$rules['all'],$rules);
|
||||||
}
|
}
|
||||||
if(!empty($rules)) {
|
if(!empty($rules)) {
|
||||||
// 分隔符替换 确保路由定义使用统一的分隔符
|
|
||||||
$regx = str_replace(Config::get('pathinfo_depr'),'/',$regx);
|
|
||||||
foreach ($rules as $rule=>$route){
|
foreach ($rules as $rule=>$route){
|
||||||
if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由
|
if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由
|
||||||
if($route instanceof \Closure) {
|
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和规则路由是否匹配
|
// 检测URL和规则路由是否匹配
|
||||||
@@ -131,31 +184,6 @@ class Route {
|
|||||||
return true;
|
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...'
|
// '路由规则'=>'[模块/控制器/操作]?额外参数1=值1&额外参数2=值2...'
|
||||||
// '路由规则'=>array('[模块/控制器/操作]','额外参数1=值1&额外参数2=值2...')
|
// '路由规则'=>array('[模块/控制器/操作]','额外参数1=值1&额外参数2=值2...')
|
||||||
@@ -196,7 +224,7 @@ class Route {
|
|||||||
exit;
|
exit;
|
||||||
}else{
|
}else{
|
||||||
// 解析路由地址
|
// 解析路由地址
|
||||||
$var = self::parseUrl($url);
|
$var = self::parseRoute($url);
|
||||||
// 解析路由地址里面的动态参数
|
// 解析路由地址里面的动态参数
|
||||||
$values = array_values($matches);
|
$values = array_values($matches);
|
||||||
foreach ($var as $key=>$val){
|
foreach ($var as $key=>$val){
|
||||||
@@ -216,7 +244,6 @@ class Route {
|
|||||||
}
|
}
|
||||||
$_GET = array_merge($var,$_GET);
|
$_GET = array_merge($var,$_GET);
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析正则路由
|
// 解析正则路由
|
||||||
@@ -236,7 +263,7 @@ class Route {
|
|||||||
exit;
|
exit;
|
||||||
}else{
|
}else{
|
||||||
// 解析路由地址
|
// 解析路由地址
|
||||||
$var = self::parseUrl($url);
|
$var = self::parseRoute($url);
|
||||||
// 解析剩余的URL参数
|
// 解析剩余的URL参数
|
||||||
$regx = substr_replace($regx,'',0,strlen($matches[0]));
|
$regx = substr_replace($regx,'',0,strlen($matches[0]));
|
||||||
if($regx) {
|
if($regx) {
|
||||||
@@ -249,6 +276,5 @@ class Route {
|
|||||||
}
|
}
|
||||||
$_GET = array_merge($var,$_GET);
|
$_GET = array_merge($var,$_GET);
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user