From 30a6bd7e8ab4ff1fed7fece1879aabe44894880c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 May 2016 15:16:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BResponse=E7=B1=BB=20=E6=94=B9?= =?UTF-8?q?=E8=BF=9Bview=E5=8A=A9=E6=89=8B=E5=87=BD=E6=95=B0=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20think\response\Html=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 30 +++++++--- library/think/Response.php | 9 +++ library/think/View.php | 15 +++-- library/think/response/Html.php | 85 +++++++++++++++++++++++++++++ library/think/response/Redirect.php | 17 ++++-- library/traits/controller/Jump.php | 7 ++- 6 files changed, 141 insertions(+), 22 deletions(-) create mode 100644 library/think/response/Html.php diff --git a/helper.php b/helper.php index 8fe6851f..5756dc0f 100644 --- a/helper.php +++ b/helper.php @@ -340,14 +340,14 @@ function trace($log = '[think]', $level = 'log') * 渲染模板输出 * @param string $template 模板文件 * @param array $vars 模板变量 + * @param integer $code 状态码 * @param string $type 输出类型 * @return \think\Response */ -function view($template = '', $vars = [], $type = 'html') +function view($template = '', $vars = [], $code = 200) { - $data = View::instance(Config::get('template'), Config::get('view_replace_str'))->fetch($template, $vars); - $response = Response::create($type); - return $response->data($data); + $response = new \think\response\Html(); + return $response->data($template)->vars($vars)->code($code); } /** @@ -386,10 +386,26 @@ function response($type = '', $options = []) /** * 获取\think\response\Json对象实例 - * @param array $options 参数 + * @param mixed $data 返回的数据 + * @param integer $code 状态码 + * @param array $options 参数状 * @return \think\response\Json */ -function json($options = []) +function json($data = [], $code = 200, $options = []) { - return new \think\response\Json($options); + $response = new \think\response\Json($options); + return $response->data($data)->code($code); +} + +/** + * 获取\think\response\Redirect对象实例 + * @param mixed $url 重定向地址 支持Url::build方法的地址 + * @param integer $code 状态码 + * @param array $params 额外参数 + * @return \think\response\Redirect + */ +function redirect($url = [], $code = 200, $params = []) +{ + $response = new \think\response\Redirect(); + return $response->data($url)->code($code)->params($params); } diff --git a/library/think/Response.php b/library/think/Response.php index aa1dbbd5..d173603c 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -297,4 +297,13 @@ class Response { return !empty($name) ? $this->header[$name] : $this->header; } + + /** + * 获取数据 + * @return mixed + */ + public function getData() + { + return $this->data; + } } diff --git a/library/think/View.php b/library/think/View.php index 31ccd22d..2cd90226 100644 --- a/library/think/View.php +++ b/library/think/View.php @@ -95,12 +95,13 @@ class View * 解析和获取模板内容 用于输出 * @param string $template 模板文件名或者内容 * @param array $vars 模板输出变量 + * @param array $replace 替换内容 * @param array $config 模板参数 - * @param bool $renderContent 是否渲染内容 + * @param bool $renderContent 是否渲染内容 * @return string * @throws Exception */ - public function fetch($template = '', $vars = [], $config = [], $renderContent = false) + public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false) { // 模板变量 $vars = array_merge($this->data, $vars); @@ -118,8 +119,9 @@ class View // 内容过滤标签 APP_HOOK && Hook::listen('view_filter', $content); // 允许用户自定义模板的字符串替换 - if (!empty($this->replace)) { - $content = strtr($content, $this->replace); + $replace = array_merge($this->replace, $replace); + if (!empty($replace)) { + $content = strtr($content, $replace); } return $content; } @@ -146,12 +148,13 @@ class View * @access public * @param string $content 内容 * @param array $vars 模板输出变量 + * @param array $replace 替换内容 * @param array $config 模板参数 * @return mixed */ - public function display($content, $vars = [], $config = []) + public function display($content, $vars = [], $replace = [], $config = []) { - return $this->fetch($content, $vars, $config, true); + return $this->fetch($content, $vars, $replace, $config, true); } /** diff --git a/library/think/response/Html.php b/library/think/response/Html.php new file mode 100644 index 00000000..a2dca3dd --- /dev/null +++ b/library/think/response/Html.php @@ -0,0 +1,85 @@ + +// +---------------------------------------------------------------------- + +namespace think\response; + +use think\Config; +use think\Response; +use think\View; + +class Html extends Response +{ + // 输出参数 + protected $options = []; + protected $vars = []; + protected $replace = []; + + /** + * 处理数据 + * @access protected + * @param mixed $data 要处理的数据 + * @return mixed + */ + protected function output($data) + { + // 返回JSON数据格式到客户端 包含状态信息 + return View::instance(Config::get('template'), Config::get('view_replace_str')) + ->fetch($data, $this->vars, $this->replace); + } + + /** + * 视图变量赋值 + * @access protected + * @param array $vars 模板变量 + * @return $this + */ + public function vars($vars = []) + { + $this->vars = $vars; + return $this; + } + + /** + * 模板变量赋值 + * @access public + * @param mixed $name 变量名 + * @param mixed $value 变量值 + * @return $this + */ + public function assign($name, $value = '') + { + if (is_array($name)) { + $this->vars = array_merge($this->vars, $name); + return $this; + } else { + $this->vars[$name] = $value; + } + return $this; + } + + /** + * 视图内容替换 + * @access public + * @param string|array $content 被替换内容(支持批量替换) + * @param string $replace 替换内容 + * @return $this + */ + public function replace($content, $replace = '') + { + if (is_array($content)) { + $this->replace = array_merge($this->replace, $content); + } else { + $this->replace[$content] = $replace; + } + return $this; + } + +} diff --git a/library/think/response/Redirect.php b/library/think/response/Redirect.php index 2cb90745..87f7b842 100644 --- a/library/think/response/Redirect.php +++ b/library/think/response/Redirect.php @@ -17,10 +17,10 @@ use think\Url; class Redirect extends Response { - protected $options = [ - 'http_response_code' => 301, - 'http_url_params' => [], - ]; + protected $options = []; + + // URL参数 + protected $params = []; /** * 处理数据 @@ -31,10 +31,15 @@ class Redirect extends Response protected function output($data) { $this->isExit = true; - $url = preg_match('/^(https?:|\/)/', $data) ? $data : Url::build($data, $this->options['http_url_params']); + $url = preg_match('/^(https?:|\/)/', $data) ? $data : Url::build($data, $this->params); $this->header['Location'] = $url; - $this->header['status'] = $this->options['http_response_code']; + $this->header['status'] = isset($this->header['status']) ? $this->header['status'] : 301; return; } + public function params($params = []) + { + $this->params = $params; + return $this; + } } diff --git a/library/traits/controller/Jump.php b/library/traits/controller/Jump.php index d7c3b83f..01721673 100644 --- a/library/traits/controller/Jump.php +++ b/library/traits/controller/Jump.php @@ -104,12 +104,13 @@ trait Jump * URL重定向 * @access protected * @param string $url 跳转的URL表达式 - * @param array|int $params 其它URL参数或http code + * @param integer $code http code + * @param array $params 其它URL参数 * @return void */ - public function redirect($url, $params = []) + public function redirect($url, $code = 301, $params = []) { - Response::create()->isExit(true)->redirect($url, $params); + Response::create('redirect')->data($url)->code($code)->params($params); } }