From b50eeb5e7d290873ad68bdbe24d431a73c2441e5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 11 May 2016 12:25:29 +0800 Subject: [PATCH] =?UTF-8?q?App=E7=B1=BB=E6=94=B9=E8=BF=9Brun=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=BC=A0=E5=85=A5Request=E5=AF=B9=E8=B1=A1=20?= =?UTF-8?q?=E4=BE=BF=E4=BA=8E=E6=9E=84=E9=80=A0=E8=87=AA=E5=B7=B1=E7=9A=84?= =?UTF-8?q?Request=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 21 ++++++++++++--------- library/think/Route.php | 8 +++----- start.php | 2 +- tests/thinkphp/library/think/urlTest.php | 18 +++++++++--------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/library/think/App.php b/library/think/App.php index 2d0e0623..7f5f2c21 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -23,10 +23,12 @@ class App /** * 执行应用程序 * @access public + * @param \think\Request $request Request对象 * @return void */ - public static function run() + public static function run($request) { + // 初始化应用(公共模块) self::initModule(COMMON_MODULE, Config::get()); @@ -66,10 +68,10 @@ class App } // 获取当前请求的调度信息 - $dispatch = Request::instance()->dispatch(); + $dispatch = $request->dispatch(); if (empty($dispatch)) { // 未指定调度类型 则进行URL路由检测 - $dispatch = self::route($config); + $dispatch = self::route($request, $config); } // 记录路由信息 APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); @@ -308,21 +310,22 @@ class App /** * URL路由检测(根据PATH_INFO) * @access public + * @param \think\Request $request * @param array $config * @throws Exception */ - public static function route(array $config) + public static function route($request, array $config) { - define('__INFO__', Request::instance()->pathinfo()); - define('__EXT__', Request::instance()->ext()); + define('__INFO__', $request->pathinfo()); + define('__EXT__', $request->ext()); // 检测URL禁用后缀 if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { throw new Exception('url suffix deny'); } - $_SERVER['PATH_INFO'] = Request::instance()->path(); + $_SERVER['PATH_INFO'] = $request->path(); $depr = $config['pathinfo_depr']; $result = false; // 路由检测 @@ -333,7 +336,7 @@ class App Route::register($config['route']); } // 路由检测(根据路由定义返回不同的URL调度) - $result = Route::check($_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false); + $result = Route::check($request, $_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false); if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { // 路由无效 throw new Exception('route not define '); @@ -346,7 +349,7 @@ class App //保证$_REQUEST正常取值 $_REQUEST = array_merge($_POST, $_GET, $_COOKIE); // 注册调度机制 - return Request::instance()->dispatch($result); + return $request->dispatch($result); } } diff --git a/library/think/Route.php b/library/think/Route.php index d57d1c10..7e60af01 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -11,8 +11,6 @@ namespace think; -use think\Request; - class Route { // 路由规则 @@ -358,7 +356,7 @@ class Route } // 检测URL路由 - public static function check($url, $depr = '/', $checkDomain = false) + public static function check($request, $url, $depr = '/', $checkDomain = false) { // 检测域名部署 if ($checkDomain) { @@ -429,7 +427,7 @@ class Route } $result = self::checkRule($key, $route, $url1, $pattern, $option); if (false !== $result) { - Request::instance()->route(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); + $request->route(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); return $result; } } @@ -442,7 +440,7 @@ class Route // 规则路由 $result = self::checkRule($rule, $route, $url, $pattern, $option); if (false !== $result) { - Request::instance()->route(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); + $request->route(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); return $result; } } diff --git a/start.php b/start.php index 5ad6f069..17f42442 100644 --- a/start.php +++ b/start.php @@ -64,5 +64,5 @@ if (APP_HOOK && isset($mode['tags'])) { // 是否自动运行 if (APP_AUTO_RUN) { - App::run(); + App::run(new Request()); } diff --git a/tests/thinkphp/library/think/urlTest.php b/tests/thinkphp/library/think/urlTest.php index 151cd4d6..a82c0936 100644 --- a/tests/thinkphp/library/think/urlTest.php +++ b/tests/thinkphp/library/think/urlTest.php @@ -26,17 +26,17 @@ class urlTest extends \PHPUnit_Framework_TestCase public function testBuildModule() { - Route::get('hello/:name', 'index/hello'); - Route::get('hello/:id', 'index/hello'); + Route::get('blog/:name', 'index/blog'); + Route::get('blog/:id', 'index/blog'); Config::set('pathinfo_depr', '/'); - $this->assertEquals('/hello/thinkphp', Url::build('index/hello?name=thinkphp')); - $this->assertEquals('/hello/thinkphp.html', Url::build('index/hello', 'name=thinkphp', 'html')); - $this->assertEquals('/hello/10', Url::build('index/hello?id=10')); - $this->assertEquals('/hello/10.html', Url::build('index/hello', 'id=10', 'html')); + $this->assertEquals('/blog/thinkphp', Url::build('index/blog?name=thinkphp')); + $this->assertEquals('/blog/thinkphp.html', Url::build('index/blog', 'name=thinkphp', 'html')); + $this->assertEquals('/blog/10', Url::build('index/blog?id=10')); + $this->assertEquals('/blog/10.html', Url::build('index/blog', 'id=10', 'html')); - Route::get('item-', 'good/item', [], ['item' => '\w+', 'id' => '\d+']); - $this->assertEquals('/item-thinkphp', Url::build('good/item?item=thinkphp')); - $this->assertEquals('/item-thinkphp2016', Url::build('good/item?item=thinkphp&id=2016')); + Route::get('item-', 'blog/item', [], ['name' => '\w+', 'id' => '\d+']); + $this->assertEquals('/item-thinkphp', Url::build('blog/item?name=thinkphp')); + $this->assertEquals('/item-thinkphp2016', Url::build('blog/item?name=thinkphp&id=2016')); } public function testBuildController()