From f8d7bb61c86b66f2b2ea657f387e0bfe540509ef Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 17 Jun 2016 08:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E9=99=A4APP=5FROUTE=5FON=20APP=5FROUT?= =?UTF-8?q?E=5FMUST=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 2 -- library/think/App.php | 34 +++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/base.php b/base.php index be75eed3..7e0cbbe7 100644 --- a/base.php +++ b/base.php @@ -32,8 +32,6 @@ defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀 defined('ENV_PREFIX') or define('ENV_PREFIX', 'PHP_'); // 环境变量的配置前缀 defined('IS_API') or define('IS_API', false); // 是否API接口 defined('APP_AUTO_RUN') or define('APP_AUTO_RUN', true); // 是否自动运行 -defined('APP_ROUTE_ON') or define('APP_ROUTE_ON', true); // 是否允许路由 -defined('APP_ROUTE_MUST') or define('APP_ROUTE_MUST', true); // 是否严格检查路由 defined('APP_MODE') or define('APP_MODE', 'common'); // 应用模式 默认为普通模式 // 环境常量 diff --git a/library/think/App.php b/library/think/App.php index 4f676fa2..e98c0763 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -54,6 +54,16 @@ class App */ public static $suffix = false; + /** + * @var bool 应用路由检测 + */ + protected static $routeCheck; + + /** + * @var bool 严格路由检测 + */ + protected static $routeMust; + /** * 执行应用程序 * @access public @@ -84,7 +94,7 @@ class App $dispatch = $request->dispatch(); if (empty($dispatch)) { // 未指定调度类型 则进行URL路由检测 - $dispatch = self::route($request, $config); + $dispatch = self::routeCheck($request, $config); } // 记录路由信息 self::$debug && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); @@ -311,7 +321,7 @@ class App } /** - * 初始化公共配置 + * 初始化应用 */ public static function initCommon() { @@ -418,7 +428,7 @@ class App * @return array * @throws \think\Exception */ - public static function route($request, array $config) + public static function routeCheck($request, array $config) { // 检测URL禁用后缀 if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', $request->pathinfo())) { @@ -429,7 +439,8 @@ class App $depr = $config['pathinfo_depr']; $result = false; // 路由检测 - if (APP_ROUTE_ON && !empty($config['url_route_on'])) { + $check = !is_null(self::$routeCheck) ? self::$routeCheck : $config['url_route_on']; + if ($check) { // 开启路由 if (!empty($config['route'])) { // 导入路由配置 @@ -437,7 +448,8 @@ class App } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($request, $path, $depr, $config['url_domain_deploy']); - if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { + $must = !is_null(self::$routeMust) ? self::$routeMust : $config['url_route_must']; + if ($must && false === $result) { // 路由无效 throw new HttpException(404, 'Route Not Found'); } @@ -452,4 +464,16 @@ class App return $request->dispatch($result); } + /** + * 设置应用的路由检测机制 + * @access public + * @param bool $route 是否需要检测路由 + * @param bool $must 是否强制检测路由 + * @return void + */ + public static function route($route, $must = false) + { + self::$routeCheck = $route; + self::$routeMust = $must; + } }