diff --git a/library/think/response.php b/library/think/response.php index 0fcd1a86..42cc890d 100644 --- a/library/think/response.php +++ b/library/think/response.php @@ -165,24 +165,26 @@ class Response */ public static function success($msg = '', $data = '', $url = '', $wait = 3) { - $code = 1; - if(is_numeric($msg)){ - $code = $msg; - $msg = ''; + $code = 1; + if (is_numeric($msg)) { + $code = $msg; + $msg = ''; } - $result = [ + $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, 'url' => $url ?: $_SERVER["HTTP_REFERER"], 'wait' => $wait, ]; - $type = Config::get('default_return_type'); + if (IS_AJAX) { - $type = Config::get('default_ajax_return'); + $type = Config::get('default_ajax_return'); + } else { + $type = Config::get('default_return_type'); } if ('html' == $type) { - $result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result); + $result = \think\View::instance()->fetch(Config::get('dispatch_jump_tmpl'), $result); } self::type($type); return $result; @@ -199,24 +201,26 @@ class Response */ public static function error($msg = '', $data = '', $url = '', $wait = 3) { - $code = 0; - if(is_numeric($msg)){ - $code = $msg; - $msg = ''; + $code = 0; + if (is_numeric($msg)) { + $code = $msg; + $msg = ''; } - $result = [ + $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, 'url' => $url ?: 'javascript:history.back(-1);', 'wait' => $wait, ]; - $type = Config::get('default_return_type'); + if (IS_AJAX) { - $type = Config::get('default_ajax_return'); + $type = Config::get('default_ajax_return'); + } else { + $type = Config::get('default_return_type'); } if ('html' == $type) { - $result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result); + $result = \think\View::instance()->fetch(Config::get('dispatch_jump_tmpl'), $result); } self::type($type); return $result; diff --git a/library/think/view.php b/library/think/view.php index 5122b749..17b31b0a 100644 --- a/library/think/view.php +++ b/library/think/view.php @@ -13,6 +13,8 @@ namespace think; class View { + // 视图实例 + protected static $instance = null; // 模板引擎实例 protected $engine = null; // 模板主题名称 @@ -40,13 +42,18 @@ class View $this->config($config); $this->engine($this->config['engine_type']); } + /** * 初始化视图 * @access public * @param array $config 配置参数 */ - static public function getInstance(array $config = []){ - return new self($config); + public static function instance(array $config = []) + { + if (is_null(self::$instance)) { + self::$instance = new self($config); + } + return self::$instance; } /** diff --git a/library/traits/controller/view.php b/library/traits/controller/view.php index 5ee15560..4dc744be 100644 --- a/library/traits/controller/view.php +++ b/library/traits/controller/view.php @@ -29,7 +29,7 @@ trait View { // 模板引擎参数 if (is_null($this->view)) { - $this->view = new \think\View(Config::get()); // 只能这样写,不然view会冲突 + $this->view = \think\View::instance(Config::get()); // 只能这样写,不然view会冲突 } } diff --git a/start.php b/start.php index 765aa00e..90135a8c 100644 --- a/start.php +++ b/start.php @@ -46,6 +46,10 @@ if (APP_HOOK && isset($mode['tags'])) { if (APP_AUTO_BUILD && is_file(APP_PATH . 'build.php')) { Build::run(include APP_PATH . 'build.php'); } -Loader::addNamespace('tests', TEST_PATH); -// 执行应用 -!IN_UNIT_TEST && App::run(); + +if (IN_UNIT_TEST) { + Loader::addNamespace('tests', TEST_PATH); +} else { + // 执行应用 + App::run(); +}