From a4a14b3bf154e69493a01dd71755c0e4dd04d23a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 May 2016 16:39:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Query=E7=B1=BB=20=E6=94=B9?= =?UTF-8?q?=E8=BF=9BResponse=E7=B1=BB=20=E6=94=B9=E8=BF=9B=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- library/think/Controller.php | 5 +-- library/think/Response.php | 58 +++++++++++++++++------------- library/think/db/Query.php | 10 ++++++ library/think/exception/Handle.php | 9 +---- library/think/response/Html.php | 29 ++++++++++++--- library/think/response/Json.php | 2 +- library/think/response/Jsonp.php | 1 + library/traits/controller/Jump.php | 2 +- 9 files changed, 76 insertions(+), 42 deletions(-) diff --git a/helper.php b/helper.php index 5756dc0f..1fccc242 100644 --- a/helper.php +++ b/helper.php @@ -347,7 +347,7 @@ function trace($log = '[think]', $level = 'log') function view($template = '', $vars = [], $code = 200) { $response = new \think\response\Html(); - return $response->data($template)->vars($vars)->code($code); + return $response->data($template)->render(true)->vars($vars)->code($code); } /** diff --git a/library/think/Controller.php b/library/think/Controller.php index c58360ea..ce5c0311 100644 --- a/library/think/Controller.php +++ b/library/think/Controller.php @@ -86,12 +86,13 @@ class Controller * @access public * @param string $template 模板文件名 * @param array $vars 模板输出变量 + * @param array $replace 模板替换 * @param array $config 模板参数 * @return mixed */ - public function fetch($template = '', $vars = [], $config = []) + public function fetch($template = '', $vars = [], $replace = [], $config = []) { - return $this->view->fetch($template, $vars, $config); + return $this->view->fetch($template, $vars, $replace, $config = []); } /** diff --git a/library/think/Response.php b/library/think/Response.php index d173603c..dec4f224 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -21,8 +21,10 @@ class Response protected $data; // 是否exit protected $isExit = false; - // contentType - protected $contentType = [ + // 当前的contentType + protected $contentType; + // 可用的输出类型 + protected static $contentTypes = [ 'json' => 'application/json', 'xml' => 'text/xml', 'html' => 'text/html', @@ -57,12 +59,36 @@ class Response { $type = strtolower($type ?: (IS_AJAX ? 'json' : 'html')); if (!isset(self::$instance[$type])) { - self::$instance[$type] = new static($options); - self::$instance[$type]->type($type, $options); + + $class = '\\think\\response\\' . ucfirst($type); + $response = class_exists($class) ? new $class($options) : new static($options); + + if (isset(self::$contentTypes[$type])) { + $response->contentType(self::$contentTypes[$type]); + } + + self::$instance[$type] = $response; } return self::$instance[$type]; } + /** + * 输出类型设置 + * @access public + * @param string $type 输出内容的格式类型 + * @param array $options 参数 + * @return $this + */ + public function type($type, $options = []) + { + $type = strtolower($type); + if (isset(self::$instance[$type])) { + return self::$instance[$type]; + } else { + return self::create($type, $options); + } + } + /** * 发送数据到客户端 * @access public @@ -73,6 +99,10 @@ class Response { $data = $data ?: $this->data; + if (isset($this->contentType)) { + $this->contentType($this->contentType); + } + if (is_callable($this->transform)) { $data = call_user_func_array($this->transform, [$data]); } @@ -146,26 +176,6 @@ class Response return $this; } - /** - * 输出类型设置 - * @access public - * @param string $type 输出内容的格式类型 - * @param array $options 参数 - * @return $this - */ - public function type($type, $options = []) - { - $type = strtolower($type); - if (!isset(self::$instance[$type])) { - $class = '\\think\\response\\' . ucfirst($type); - self::$instance[$type] = class_exists($class) ? new $class($options) : $this; - } - if (isset($this->contentType[$type])) { - self::$instance[$type]->contentType($this->contentType[$type]); - } - return self::$instance[$type]; - } - /** * 输出是否exit设置 * @access public diff --git a/library/think/db/Query.php b/library/think/db/Query.php index c23b242c..1c8e1cf7 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -112,6 +112,16 @@ class Query return $this->connection->execute($sql, $bind, $fetch, $getLastInsID); } + /** + * 获取最近插入的ID + * @access public + * @return string + */ + public function getLastInsID() + { + return $this->connection->getLastInsID(); + } + /** * 获取当前的builder实例对象 * @access protected diff --git a/library/think/exception/Handle.php b/library/think/exception/Handle.php index a319f9c8..371d8239 100644 --- a/library/think/exception/Handle.php +++ b/library/think/exception/Handle.php @@ -144,14 +144,7 @@ class Handle // 不显示详细错误信息 $data['message'] = Config::get('error_message'); } - ob_start(); - ob_implicit_flush(0); - extract($data); - include Config::get('exception_tmpl'); - // 获取并清空缓存 - $content = ob_get_clean(); - - $response = Response::create('html')->data($content); + $response = Response::create('html')->render(true)->data(Config::get('exception_tmpl'))->vars($data); if ($exception instanceof HttpException) { $statusCode = $exception->getStatusCode(); diff --git a/library/think/response/Html.php b/library/think/response/Html.php index a2dca3dd..8b0c90eb 100644 --- a/library/think/response/Html.php +++ b/library/think/response/Html.php @@ -18,9 +18,11 @@ use think\View; class Html extends Response { // 输出参数 - protected $options = []; - protected $vars = []; - protected $replace = []; + protected $options = []; + protected $vars = []; + protected $replace = []; + protected $contentType = 'text/html'; + protected $render = false; /** * 处理数据 @@ -31,8 +33,25 @@ class Html extends Response protected function output($data) { // 返回JSON数据格式到客户端 包含状态信息 - return View::instance(Config::get('template'), Config::get('view_replace_str')) - ->fetch($data, $this->vars, $this->replace); + if ($this->render) { + // 渲染模板输出 + return View::instance(Config::get('template'), Config::get('view_replace_str')) + ->fetch($data, $this->vars, $this->replace, [], false, false); + } else { + return $data; + } + } + + /** + * 是否需要进行视图渲染 + * @access protected + * @param bool $render 是否渲染 + * @return $this + */ + public function render($render = true) + { + $this->render = $render; + return $this; } /** diff --git a/library/think/response/Json.php b/library/think/response/Json.php index 963221bc..23b68ea6 100644 --- a/library/think/response/Json.php +++ b/library/think/response/Json.php @@ -19,7 +19,7 @@ class Json extends Response protected $options = [ 'json_encode_param' => JSON_UNESCAPED_UNICODE, ]; - + protected $contentType = 'application/json'; /** * 处理数据 * @access protected diff --git a/library/think/response/Jsonp.php b/library/think/response/Jsonp.php index 8bfc8907..e07efbf4 100644 --- a/library/think/response/Jsonp.php +++ b/library/think/response/Jsonp.php @@ -21,6 +21,7 @@ class Jsonp extends Response 'default_jsonp_handler' => 'jsonpReturn', 'json_encode_param' => JSON_UNESCAPED_UNICODE, ]; + protected $contentType = 'application/json'; /** * 处理数据 diff --git a/library/traits/controller/Jump.php b/library/traits/controller/Jump.php index 01721673..8cb072fc 100644 --- a/library/traits/controller/Jump.php +++ b/library/traits/controller/Jump.php @@ -110,7 +110,7 @@ trait Jump */ public function redirect($url, $code = 301, $params = []) { - Response::create('redirect')->data($url)->code($code)->params($params); + return Response::create('redirect')->data($url)->code($code)->params($params); } }