改写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)
{
$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;

View File

@@ -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;
}
/**

View File

@@ -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会冲突
}
}

View File

@@ -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();
}