From fb15a8f4874eb09b3298392046e29d27a40a53bb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 25 Mar 2013 22:07:55 +0800 Subject: [PATCH] =?UTF-8?q?Think\Route=E7=9A=84add=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=9B=B4=E5=90=8D=E4=B8=BAregister=E6=96=B9=E6=B3=95=20?= =?UTF-8?q?=E5=B9=B6=E6=94=AF=E6=8C=81=E6=89=B9=E9=87=8F=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=88=E4=BB=85=E9=99=90=E4=BA=8E=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E8=AF=B7=E6=B1=82=E7=B1=BB=E5=9E=8B=EF=BC=89=20?= =?UTF-8?q?=E5=90=8C=E6=97=B6=20get=20post=20put=20delete=20=E5=92=8Cany?= =?UTF-8?q?=20=E6=96=B9=E6=B3=95=E5=9D=87=E6=94=AF=E6=8C=81=20=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E6=B3=A8=E5=86=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Think/Route.php | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Think/Route.php b/Think/Route.php index 864e6dbf..b0b5292e 100644 --- a/Think/Route.php +++ b/Think/Route.php @@ -13,11 +13,11 @@ namespace Think; class Route { // 路由规则 static private $rules = [ - 'get' => [], - 'post' => [], - 'put' => [], - 'delete' => [], - 'all' => [], + 'GET' => [], + 'POST' => [], + 'PUT' => [], + 'DELETE' => [], + '*' => [], ]; // 映射规则 @@ -32,45 +32,47 @@ class Route { } } - // 添加某个路由规则 - static public function add($rule,$route,$type='get'){ + // 注册某个路由规则 + static public function register($rule,$route='',$type='GET'){ if(is_array($type)) { foreach ($type as $val){ self::$rules[$val][$rule] = $route; } + }elseif(is_array($rule)) { + self::$rules[$type] = array_merge(self::$rules[$type],$rule); }else{ self::$rules[$type][$rule] = $route; } } // 导入路由规则 - static public function import($rules,$type='get'){ + static public function import($rules,$type='GET'){ self::$rules[$type] = array_merge(self::$rules[$type],$rules); } // 添加一条任意请求的路由规则 - static public function any($rule,$route){ - self::add($rule,$route,'all'); + static public function any($rule,$route=''){ + self::register($rule,$route,'*'); } // 添加一条get请求的路由规则 - static public function get($rule,$route){ - self::add($rule,$route,'get'); + static public function get($rule,$route=''){ + self::register($rule,$route,'GET'); } // 添加一条post请求的路由规则 - static public function post($rule,$route){ - self::add($rule,$route,'post'); + static public function post($rule,$route=''){ + self::register($rule,$route,'POST'); } // 添加一条put请求的路由规则 - static public function put($rule,$route){ - self::add($rule,$route,'put'); + static public function put($rule,$route=''){ + self::register($rule,$route,'PUT'); } // 添加一条delete请求的路由规则 - static public function delete($rule,$route){ - self::add($rule,$route,'delete'); + static public function delete($rule,$route=''){ + self::register($rule,$route,'DELETE'); } // 检测URL路由 @@ -83,9 +85,9 @@ class Route { 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); + $rules = self::$rules[$_SERVER['REQUEST_METHOD']]; + if(!empty(self::$rules['*'])) { + $rules = array_merge(self::$rules['*'],$rules); } if(!empty($rules)) { foreach ($rules as $rule=>$route){