From a28899656b27c48c7af1717ffcc54c8a638d80f3 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 25 Mar 2013 14:37:02 +0800 Subject: [PATCH] =?UTF-8?q?Think\Route=E7=B1=BB=E6=94=B9=E8=BF=9B=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0add=E7=AD=89=E6=96=B9=E6=B3=95=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=B3=A8=E5=86=8C=E8=B7=AF=E7=94=B1=E8=A7=84=E5=88=99?= =?UTF-8?q?=20=E5=B9=B6=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=94=AF=E6=8C=81=20=E8=B7=AF=E7=94=B1=E8=A7=84?= =?UTF-8?q?=E5=88=99=E6=94=AF=E6=8C=81=E9=97=AD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Think/App.php | 3 +-- Think/Route.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/Think/App.php b/Think/App.php index 4ef36741..897e0314 100644 --- a/Think/App.php +++ b/Think/App.php @@ -247,8 +247,7 @@ class App { Tag::listen('path_info'); $url = trim(substr_replace($_SERVER['PATH_INFO'],'',0,strlen($_GET[$var_m])+1),'/'); // 模块路由检测 - if(!$config['url_route'] || !Route::check($url,$config['url_route_rules'])){ - // PATHINFO URL规则 默认为 Controller/Action/ + 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); diff --git a/Think/Route.php b/Think/Route.php index 7dacb124..09e0895a 100644 --- a/Think/Route.php +++ b/Think/Route.php @@ -11,16 +11,76 @@ // $Id$ namespace Think; class Route { + // 路由规则 + static private $rules = [ + 'get' => [], + 'post' => [], + 'put' => [], + 'delete' => [], + 'all' => [], + ]; - static public function check($regx,$rules) { + // 添加某个路由规则 + static public function add($rule,$route,$type='get'){ + if(is_array($type)) { + foreach ($type as $val){ + self::$rules[$val][$rule] = $route; + } + }else{ + self::$rules[$type][$rule] = $route; + } + } + + // 导入路由规则 + static public function import($rules){ + foreach ($rules as $type=>$rule){ + self::$rules[$type] = array_merge(self::$rules[$type],$rule); + } + } + + // 添加一条任意请求的路由规则 + static public function any($rule,$route){ + self::add($rule,$route,'all'); + } + + // 添加一条get请求的路由规则 + static public function get($rule,$route){ + self::add($rule,$route,'get'); + } + + // 添加一条post请求的路由规则 + static public function post($rule,$route){ + self::add($rule,$route,'post'); + } + + // 添加一条put请求的路由规则 + static public function put($rule,$route){ + self::add($rule,$route,'put'); + } + + // 添加一条delete请求的路由规则 + static public function delete($rule,$route){ + self::add($rule,$route,'delete'); + } + + // 检测URL路由 + static public function check($regx) { // 优先检测是否存在PATH_INFO if(empty($regx)) return true; // 路由处理 + $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) { + call_user_func($route); + exit; + } return self::parseRegex($matches,$route,$regx); }else{ // 规则路由 $len1 = substr_count($regx,'/'); @@ -33,7 +93,11 @@ class Route { $rule = substr($rule,0,-1); } } - if(self::checkUrlMatch($regx,$rule)){ + if(self::match($regx,$rule)){ + if($route instanceof \Closure) { + call_user_func($route); + exit; + } return self::parseRule($rule,$route,$regx); } } @@ -44,7 +108,7 @@ class Route { } // 检测URL和规则路由是否匹配 - static private function checkUrlMatch($regx,$rule) { + static private function match($regx,$rule) { $m1 = explode('/',$regx); $m2 = explode('/',$rule); foreach ($m2 as $key=>$val){