改进Response类 增加think\response\View类 用于渲染模板响应输出

This commit is contained in:
thinkphp
2016-05-16 18:10:46 +08:00
parent 4870d743ac
commit c7c94258d0
9 changed files with 92 additions and 143 deletions

View File

@@ -13,6 +13,8 @@ namespace think;
use think\exception\HttpResponseException;
use think\Response;
use think\response\Json;
use think\response\Jsonp;
/**
* App 应用管理
@@ -117,8 +119,20 @@ class App
// 监听app_end
APP_HOOK && Hook::listen('app_end', $data);
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
switch ($type) {
case 'json':
$response = new Json($data);
break;
case 'jsonp':
$response = new Jsonp($data);
break;
case 'html':
default:
$response = new Response($data, $type);
break;
}
// 自动响应输出
return Response::create($type)->send($data);
return $response->send();
}
}
}

View File

@@ -19,12 +19,13 @@ class Response
protected $transform;
// 输出数据
protected $data;
protected $type;
// 是否exit
protected $isExit = false;
// 当前的contentType
protected $contentType;
// 可用的输出类型
protected static $contentTypes = [
protected $contentTypes = [
'json' => 'application/json',
'xml' => 'text/xml',
'html' => 'text/html',
@@ -43,52 +44,18 @@ class Response
* @access public
* @param array $options 参数
*/
public function __construct($options = [])
public function __construct($data = [], $type = '', $options = [])
{
$this->data = $data;
$this->type = strtolower($type ?: (IS_AJAX ? 'json' : 'html'));
if (isset($this->contentTypes[$this->type])) {
$this->contentType($this->contentTypes[$this->type]);
}
$this->options = array_merge($this->options, $options);
}
/**
* 创建一个response对象
* @access public
* @param string $type 输出类型
* @param array $options 参数
* @return \think\Response
*/
public static function create($type = '', $options = [])
{
$type = strtolower($type ?: (IS_AJAX ? 'json' : 'html'));
if (!isset(self::$instance[$type])) {
$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

View File

@@ -12,6 +12,7 @@
namespace think\controller;
use think\Response;
use think\response\Json;
abstract class Rest
{
@@ -90,9 +91,13 @@ abstract class Rest
* @param integer $code HTTP状态
* @return void
*/
protected function response($data, $type = '', $code = 200)
protected function response($data, $type = 'json', $code = 200)
{
return Response::create($type)->data($data)->code($code);
if('json'==$type){
return new Json($data)->code($code);
}else{
return new Response($data,$type)->code($code);
}
}
/**

View File

@@ -151,7 +151,7 @@ class Handle
// 获取并清空缓存
$content = ob_get_clean();
$response = Response::create('html')->data($content);
$response = new Response($content, 'html');
if ($exception instanceof HttpException) {
$statusCode = $exception->getStatusCode();

View File

@@ -21,7 +21,7 @@ class Jsonp extends Response
'default_jsonp_handler' => 'jsonpReturn',
'json_encode_param' => JSON_UNESCAPED_UNICODE,
];
protected $contentType = 'application/json';
protected $contentType = 'application/javascript';
/**
* 处理数据

View File

@@ -13,16 +13,15 @@ namespace think\response;
use think\Config;
use think\Response;
use think\View;
use think\View as ViewTemplate;
class Html extends Response
class View extends Response
{
// 输出参数
protected $options = [];
protected $vars = [];
protected $replace = [];
protected $contentType = 'text/html';
protected $render = false;
/**
* 处理数据
@@ -32,26 +31,9 @@ class Html extends Response
*/
protected function output($data)
{
// 返回JSON数据格式到客户端 包含状态信息
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;
// 渲染模板输出
return ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
->fetch($data, $this->vars, $this->replace);
}
/**