From abeb4dca94c8b6398a942a464be956e2060d9f1f Mon Sep 17 00:00:00 2001 From: huangdijia Date: Fri, 4 Dec 2015 23:02:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E9=A2=9D=E5=A4=96=E5=8F=82=E6=95=B0=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/route.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/library/think/route.php b/library/think/route.php index de1712ba..2989a7da 100644 --- a/library/think/route.php +++ b/library/think/route.php @@ -446,19 +446,17 @@ class Route parse_str($url, $var); } if (isset($path)) { - $params = []; + // 解析path额外的参数 if (!empty($path[3])) { - $params = explode('/', array_pop($path)); + preg_replace_callback('/([^\/]+)\/([^\/]+)/', function ($match) use (&$var) { + $var[strtolower($match[1])] = strip_tags($match[2]); + }, 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; - // 解析path额外的参数 - for ($i = 0; $i < count($params); $i++) { - $var[$params[$i]] = $params[++$i]; - } } return ['route' => [$module, $controller, $action], 'var' => $var]; } From 178a39af4471bb7352b12534ab2f304741f156cf Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 5 Dec 2015 20:37:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E7=B1=BB=E7=9A=84parseUrl=E6=96=B9=E6=B3=95=E5=AF=B9=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=8D=E5=90=8C=E7=9A=84pathinfo=5Fdepr=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/app.php | 31 +++++++++++++++++++------------ library/think/route.php | 9 ++++++--- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/library/think/app.php b/library/think/app.php index 56a6e7f0..043d386d 100644 --- a/library/think/app.php +++ b/library/think/app.php @@ -225,16 +225,13 @@ class App // CLI模式下 index.php module/controller/action/params/... $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : ''; } - // [模块,控制器,操作] - $result = [null, null, null]; // 检测域名部署 if (!IS_CLI && !empty($config['domain_deploy'])) { if ($match = Route::checkDomain($config['domain_rules'])) { - $result = $match; - (!defined('BIND_MODULE') && !empty($result[0])) && define('BIND_MODULE', $result[0]); - (!defined('BIND_CONTROLLER') && !empty($result[1])) && define('BIND_CONTROLLER', $result[1]); - (!defined('BIND_ACTION') && !empty($result[2])) && define('BIND_ACTION', $result[2]); + (!defined('BIND_MODULE') && !empty($match[0])) && define('BIND_MODULE', $match[0]); + (!defined('BIND_CONTROLLER') && !empty($match[1])) && define('BIND_CONTROLLER', $match[1]); + (!defined('BIND_ACTION') && !empty($match[2])) && define('BIND_ACTION', $match[2]); } } @@ -256,6 +253,9 @@ class App } } + // [模块,控制器,操作] + $result = [null, null, null]; + if (empty($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; define('__INFO__', ''); @@ -265,28 +265,35 @@ class App define('__INFO__', $_SERVER['PATH_INFO']); // URL后缀 define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION))); - $_SERVER['PATH_INFO'] = __INFO__; if (__INFO__) { if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { throw new Exception('URL_SUFFIX_DENY'); } + $depr = $config['pathinfo_depr']; // 还原劫持后真实pathinfo - $path_info = (defined('BIND_MODULE')?BIND_MODULE.'/':'') . (defined('BIND_CONTROLLER')?BIND_CONTROLLER.'/':'') . (defined('BIND_ACTION')?BIND_ACTION.'/':'') . __INFO__; + $path_info = + (defined('BIND_MODULE') ? BIND_MODULE . $depr : '') . + (defined('BIND_CONTROLLER') ? BIND_CONTROLLER . $depr : '') . + (defined('BIND_ACTION') ? BIND_ACTION . $depr : '') . + __INFO__; + // 路由检测 if (!empty($config['url_route_on'])) { // 开启路由 则检测路由配置 Route::register($config['route']); - $result = Route::check($path_info, $config['pathinfo_depr']); + $result = Route::check($path_info, $depr); if (false === $result) { // 路由无效 if ($config['url_route_must']) { throw new Exception('route not define '); } else { - $result = Route::parseUrl($path_info); + // 继续分析URL + $result = Route::parseUrl($path_info, $depr); } } } else { - $result = Route::parseUrl($path_info); + // 分析URL地址 + $result = Route::parseUrl($path_info, $depr); } } // 去除URL后缀 @@ -325,7 +332,7 @@ class App define('CONTROLLER_NAME', defined('BIND_CONTROLLER') ? BIND_CONTROLLER : $controller); // 获取操作名 - $action = strip_tags(strtolower($result[2] ?: $config['default_action'])); + $action = strip_tags(strtolower($result[2] ?: $config['default_action'])); define('ACTION_NAME', defined('BIND_ACTION') ? BIND_ACTION : $action); } } diff --git a/library/think/route.php b/library/think/route.php index 2989a7da..251006f7 100644 --- a/library/think/route.php +++ b/library/think/route.php @@ -217,13 +217,12 @@ class Route } // 检测URL路由 - public static function check($url, $depr = '/', &$result = []) + public static function check($url, $depr = '/') { // 优先检测是否存在PATH_INFO if (empty($url)) { $url = '/'; } - // 分隔符替换 确保路由定义使用统一的分隔符 if ('/' != $depr) { $url = str_replace($depr, '/', $url); @@ -419,8 +418,12 @@ class Route } // 解析模块的URL地址 [模块/控制器/操作?]参数1=值1&参数2=值2... - public static function parseUrl($url) + public static function parseUrl($url, $depr = '/') { + // 分隔符替换 确保路由定义使用统一的分隔符 + if ('/' != $depr) { + $url = str_replace($depr, '/', $url); + } $result = self::parseRoute($url); if (!empty($result['var'])) { $_GET = array_merge($result['var'], $_GET);