From d122bad9b6976eb12c1da8a493c7bf7af031669a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 29 May 2016 23:07:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0http=E5=8A=A9=E6=89=8B?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20=E6=94=B9=E8=BF=9BApp=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 11 +++++++++++ library/think/App.php | 5 +++-- library/think/Loader.php | 4 +++- library/think/log/driver/Trace.php | 2 +- mode/console/App.php | 2 -- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/helper.php b/helper.php index f7ac2303..7f2084b0 100644 --- a/helper.php +++ b/helper.php @@ -419,3 +419,14 @@ function redirect($url = [], $params = [], $code = 302) } return Response::create($url, 'redirect')->code($code)->params($params); } + +/** + * 抛出HTTP异常 + * @param integer $code 状态码 + * @param string $message 错误信息 + * @param array $header 参数 + */ +function http($code, $message = null, $header = []) +{ + throw new \think\exception\HttpException($code, $message, null, $header); +} diff --git a/library/think/App.php b/library/think/App.php index 8aa14a43..4feae3fc 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -13,6 +13,7 @@ namespace think; use think\Config; use think\Exception; +use think\exception\HttpException; use think\exception\HttpResponseException; use think\Hook; use think\Lang; @@ -249,7 +250,7 @@ class App // 初始化模块 $config = self::initModule(MODULE_NAME, $config); } else { - throw new Exception('module [ ' . MODULE_NAME . ' ] not exists ', 10005); + throw new HttpException(404,'module [ ' . MODULE_NAME . ' ] not exists '); } } else { // 单一模块部署 @@ -383,7 +384,7 @@ class App $result = Route::check($request, $path, $depr, !IS_CLI ? $config['url_domain_deploy'] : false); if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { // 路由无效 - throw new Exception('route not define '); + throw new HttpException(404, 'Not Found'); } } if (false === $result) { diff --git a/library/think/Loader.php b/library/think/Loader.php index f271a6b4..f0416ffc 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -11,6 +11,8 @@ namespace think; +use think\exception\HttpException; + class Loader { // 类名映射 @@ -319,7 +321,7 @@ class Loader } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { return new $emptyClass(Request::instance()); } else { - throw new Exception('class [ ' . $class . ' ] not exists', 10001); + throw new HttpException(404, 'class [ ' . $class . ' ] not exists'); } } diff --git a/library/think/log/driver/Trace.php b/library/think/log/driver/Trace.php index 6c7dfa78..58f1f71d 100644 --- a/library/think/log/driver/Trace.php +++ b/library/think/log/driver/Trace.php @@ -41,7 +41,7 @@ class Trace */ public function save(array $log = []) { - if (IS_AJAX || IS_CLI || IS_API || (defined('RESPONSE_TYPE') && !in_array(RESPONSE_TYPE, ['html', 'view']))) { + if (IS_CLI || IS_AJAX || IS_API || (defined('RESPONSE_TYPE') && !in_array(RESPONSE_TYPE, ['html', 'view']))) { // ajax cli api方式下不输出 return false; } diff --git a/mode/console/App.php b/mode/console/App.php index 9b97b58a..7376a84a 100644 --- a/mode/console/App.php +++ b/mode/console/App.php @@ -24,8 +24,6 @@ class App { self::init(); - define('IS_AJAX', false); - // 实例化console $console = new Console('Think Console', '0.1'); // 读取指令集