增加http助手函数 改进App类

This commit is contained in:
thinkphp
2016-05-29 23:07:24 +08:00
parent eadaef6425
commit d122bad9b6
5 changed files with 18 additions and 6 deletions

View File

@@ -419,3 +419,14 @@ function redirect($url = [], $params = [], $code = 302)
} }
return Response::create($url, 'redirect')->code($code)->params($params); 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);
}

View File

@@ -13,6 +13,7 @@ namespace think;
use think\Config; use think\Config;
use think\Exception; use think\Exception;
use think\exception\HttpException;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
use think\Hook; use think\Hook;
use think\Lang; use think\Lang;
@@ -249,7 +250,7 @@ class App
// 初始化模块 // 初始化模块
$config = self::initModule(MODULE_NAME, $config); $config = self::initModule(MODULE_NAME, $config);
} else { } else {
throw new Exception('module [ ' . MODULE_NAME . ' ] not exists ', 10005); throw new HttpException(404'module [ ' . MODULE_NAME . ' ] not exists ');
} }
} else { } else {
// 单一模块部署 // 单一模块部署
@@ -383,7 +384,7 @@ class App
$result = Route::check($request, $path, $depr, !IS_CLI ? $config['url_domain_deploy'] : false); $result = Route::check($request, $path, $depr, !IS_CLI ? $config['url_domain_deploy'] : false);
if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { 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) { if (false === $result) {

View File

@@ -11,6 +11,8 @@
namespace think; namespace think;
use think\exception\HttpException;
class Loader class Loader
{ {
// 类名映射 // 类名映射
@@ -319,7 +321,7 @@ class Loader
} elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) {
return new $emptyClass(Request::instance()); return new $emptyClass(Request::instance());
} else { } else {
throw new Exception('class [ ' . $class . ' ] not exists', 10001); throw new HttpException(404, 'class [ ' . $class . ' ] not exists');
} }
} }

View File

@@ -41,7 +41,7 @@ class Trace
*/ */
public function save(array $log = []) 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方式下不输出 // ajax cli api方式下不输出
return false; return false;
} }

View File

@@ -24,8 +24,6 @@ class App
{ {
self::init(); self::init();
define('IS_AJAX', false);
// 实例化console // 实例化console
$console = new Console('Think Console', '0.1'); $console = new Console('Think Console', '0.1');
// 读取指令集 // 读取指令集