改写View类的instance方法

This commit is contained in:
thinkphp
2015-12-24 16:15:19 +08:00
parent 0f57228759
commit 222f913eb0
4 changed files with 37 additions and 22 deletions

View File

@@ -165,24 +165,26 @@ class Response
*/ */
public static function success($msg = '', $data = '', $url = '', $wait = 3) public static function success($msg = '', $data = '', $url = '', $wait = 3)
{ {
$code = 1; $code = 1;
if(is_numeric($msg)){ if (is_numeric($msg)) {
$code = $msg; $code = $msg;
$msg = ''; $msg = '';
} }
$result = [ $result = [
'code' => $code, 'code' => $code,
'msg' => $msg, 'msg' => $msg,
'data' => $data, 'data' => $data,
'url' => $url ?: $_SERVER["HTTP_REFERER"], 'url' => $url ?: $_SERVER["HTTP_REFERER"],
'wait' => $wait, 'wait' => $wait,
]; ];
$type = Config::get('default_return_type');
if (IS_AJAX) { 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) { 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); self::type($type);
return $result; return $result;
@@ -199,24 +201,26 @@ class Response
*/ */
public static function error($msg = '', $data = '', $url = '', $wait = 3) public static function error($msg = '', $data = '', $url = '', $wait = 3)
{ {
$code = 0; $code = 0;
if(is_numeric($msg)){ if (is_numeric($msg)) {
$code = $msg; $code = $msg;
$msg = ''; $msg = '';
} }
$result = [ $result = [
'code' => $code, 'code' => $code,
'msg' => $msg, 'msg' => $msg,
'data' => $data, 'data' => $data,
'url' => $url ?: 'javascript:history.back(-1);', 'url' => $url ?: 'javascript:history.back(-1);',
'wait' => $wait, 'wait' => $wait,
]; ];
$type = Config::get('default_return_type');
if (IS_AJAX) { 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) { 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); self::type($type);
return $result; return $result;

View File

@@ -13,6 +13,8 @@ namespace think;
class View class View
{ {
// 视图实例
protected static $instance = null;
// 模板引擎实例 // 模板引擎实例
protected $engine = null; protected $engine = null;
// 模板主题名称 // 模板主题名称
@@ -40,13 +42,18 @@ class View
$this->config($config); $this->config($config);
$this->engine($this->config['engine_type']); $this->engine($this->config['engine_type']);
} }
/** /**
* 初始化视图 * 初始化视图
* @access public * @access public
* @param array $config 配置参数 * @param array $config 配置参数
*/ */
static public function getInstance(array $config = []){ public static function instance(array $config = [])
return new self($config); {
if (is_null(self::$instance)) {
self::$instance = new self($config);
}
return self::$instance;
} }
/** /**

View File

@@ -29,7 +29,7 @@ trait View
{ {
// 模板引擎参数 // 模板引擎参数
if (is_null($this->view)) { if (is_null($this->view)) {
$this->view = new \think\View(Config::get()); // 只能这样写不然view会冲突 $this->view = \think\View::instance(Config::get()); // 只能这样写不然view会冲突
} }
} }

View File

@@ -46,6 +46,10 @@ if (APP_HOOK && isset($mode['tags'])) {
if (APP_AUTO_BUILD && is_file(APP_PATH . 'build.php')) { if (APP_AUTO_BUILD && is_file(APP_PATH . 'build.php')) {
Build::run(include APP_PATH . 'build.php'); Build::run(include APP_PATH . 'build.php');
} }
Loader::addNamespace('tests', TEST_PATH);
// 执行应用 if (IN_UNIT_TEST) {
!IN_UNIT_TEST && App::run(); Loader::addNamespace('tests', TEST_PATH);
} else {
// 执行应用
App::run();
}