mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 15:02:47 +08:00
response类增加create方法 改进think\response\redirect类 改进助手函数和traits\controller\Jump
This commit is contained in:
28
helper.php
28
helper.php
@@ -361,13 +361,14 @@ function request()
|
||||
|
||||
/**
|
||||
* 创建Response对象实例
|
||||
* @param mixed $data 输出数据
|
||||
* @param string $type 输出类型
|
||||
* @param array $options 参数
|
||||
* @return \think\Response
|
||||
*/
|
||||
function response($data = [], $type = '', $options = [])
|
||||
{
|
||||
return new Response($data, $type, $options);
|
||||
return Response::create($data, $type, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,8 +380,7 @@ function response($data = [], $type = '', $options = [])
|
||||
*/
|
||||
function view($template = '', $vars = [], $code = 200)
|
||||
{
|
||||
$response = new \think\response\View();
|
||||
return $response->data($template)->vars($vars)->code($code);
|
||||
return Response::create($template, 'view')->vars($vars)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -392,8 +392,19 @@ function view($template = '', $vars = [], $code = 200)
|
||||
*/
|
||||
function json($data = [], $code = 200, $options = [])
|
||||
{
|
||||
$response = new \think\response\Json($options);
|
||||
return $response->data($data)->code($code);
|
||||
return Response::create($data, 'json', $options)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取\think\response\Jsonp对象实例
|
||||
* @param mixed $data 返回的数据
|
||||
* @param integer $code 状态码
|
||||
* @param array $options 参数
|
||||
* @return \think\response\Jsonp
|
||||
*/
|
||||
function jsonp($data = [], $code = 200, $options = [])
|
||||
{
|
||||
return Response::create($data, 'jsonp', $options)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -405,8 +416,7 @@ function json($data = [], $code = 200, $options = [])
|
||||
*/
|
||||
function xml($data = [], $code = 200, $options = [])
|
||||
{
|
||||
$response = new \think\response\Xml($options);
|
||||
return $response->data($data)->code($code);
|
||||
return Response::create($data, 'xml', $options)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -418,6 +428,6 @@ function xml($data = [], $code = 200, $options = [])
|
||||
*/
|
||||
function redirect($url = [], $code = 200, $params = [])
|
||||
{
|
||||
$response = new \think\response\Redirect();
|
||||
return $response->data($url)->code($code)->params($params);
|
||||
$response = Response::create($url, 'redirect')->code($code)->params($params);
|
||||
throw new HttpResponseException($response);
|
||||
}
|
||||
|
||||
@@ -117,12 +117,7 @@ class App
|
||||
// 监听app_end
|
||||
APP_HOOK && Hook::listen('app_end', $data);
|
||||
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
||||
if (in_array(strtolower($type), ['json', 'jsonp', 'xml'])) {
|
||||
$class = '\\think\\response\\' . ucfirst($type);
|
||||
} else {
|
||||
$class = '\\think\\Response';
|
||||
}
|
||||
return (new $class($data, $type))->send();
|
||||
return Response::create($data, $type)->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@ class Response
|
||||
// 输出数据
|
||||
protected $data;
|
||||
protected $type;
|
||||
// 是否exit
|
||||
protected $isExit = false;
|
||||
// 当前的contentType
|
||||
protected $contentType;
|
||||
// 可用的输出类型
|
||||
@@ -42,7 +40,9 @@ class Response
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $options 参数
|
||||
* @param mixed $data 输出数据
|
||||
* @param string $type 输出类型
|
||||
* @param array $options 输出参数
|
||||
*/
|
||||
public function __construct($data = [], $type = '', $options = [])
|
||||
{
|
||||
@@ -56,6 +56,28 @@ class Response
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Response对象
|
||||
* @access public
|
||||
* @param mixed $data 输出数据
|
||||
* @param string $type 输出类型
|
||||
* @param array $options 输出参数
|
||||
*/
|
||||
public static function create($data = [], $type = '', $options = [])
|
||||
{
|
||||
$type = strtolower($type ?: (IS_AJAX ? 'json' : 'html'));
|
||||
if (!isset(self::$instance[$type])) {
|
||||
$class = '\\think\response\\' . ucfirst($type);
|
||||
if (class_exists($class)) {
|
||||
$response = new $class($data, $type, $options);
|
||||
} else {
|
||||
$response = new static($data, $type, $options);
|
||||
}
|
||||
self::$instance[$type] = $response;
|
||||
}
|
||||
return self::$instance[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送数据到客户端
|
||||
* @access public
|
||||
@@ -89,11 +111,7 @@ class Response
|
||||
}
|
||||
}
|
||||
echo $data;
|
||||
if ($this->isExit) {
|
||||
exit;
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,18 +161,6 @@ class Response
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出是否exit设置
|
||||
* @access public
|
||||
* @param bool $exit 是否退出
|
||||
* @return $this
|
||||
*/
|
||||
public function isExit($exit)
|
||||
{
|
||||
$this->isExit = (boolean) $exit;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回封装后的API数据到客户端
|
||||
* @access public
|
||||
@@ -283,4 +289,13 @@ class Response
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取输出类型
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
namespace think\controller;
|
||||
|
||||
use think\Response;
|
||||
use think\response\Json;
|
||||
|
||||
abstract class Rest
|
||||
{
|
||||
@@ -93,11 +92,7 @@ abstract class Rest
|
||||
*/
|
||||
protected function response($data, $type = 'json', $code = 200)
|
||||
{
|
||||
if('json'==$type){
|
||||
return (new Json($data))->code($code);
|
||||
}else{
|
||||
return (new Response($data,$type))->code($code);
|
||||
}
|
||||
return Response::create($data, $type)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,10 +30,11 @@ class Redirect extends Response
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
$this->isExit = true;
|
||||
$url = preg_match('/^(https?:|\/)/', $data) ? $data : Url::build($data, $this->params);
|
||||
$this->header['Location'] = $url;
|
||||
$this->header['status'] = isset($this->header['status']) ? $this->header['status'] : 301;
|
||||
$this->isExit = true;
|
||||
$url = preg_match('/^(https?:|\/)/', $data) ? $data : Url::build($data, $this->params);
|
||||
$this->header['Location'] = $url;
|
||||
$this->header['status'] = isset($this->header['status']) ? $this->header['status'] : 301;
|
||||
$this->header['Cache-control'] = 'no-cache,must-revalidate';
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@ namespace traits\controller;
|
||||
use think\Config;
|
||||
use think\exception\HttpResponseException;
|
||||
use think\Response;
|
||||
use think\response\Json;
|
||||
use think\response\Jsonp;
|
||||
use think\response\Redirect;
|
||||
use think\View as ViewTemplate;
|
||||
|
||||
@@ -49,21 +47,11 @@ trait Jump
|
||||
];
|
||||
|
||||
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
||||
|
||||
switch (strtolower($type)) {
|
||||
case 'json':
|
||||
$response = new Json($result);
|
||||
break;
|
||||
case 'jsonp':
|
||||
$response = new Jsonp($result);
|
||||
break;
|
||||
case 'html':
|
||||
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch(Config::get('dispatch_success_tmpl'), $result);
|
||||
default:
|
||||
$response = new Response($result, $type);
|
||||
if ('html' == strtolower($type)) {
|
||||
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch(Config::get('dispatch_success_tmpl'), $result);
|
||||
}
|
||||
return $response;
|
||||
return Response::create($result, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,20 +79,11 @@ trait Jump
|
||||
];
|
||||
|
||||
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
||||
|
||||
switch (strtolower($type)) {
|
||||
case 'json':
|
||||
$response = new Json($result);
|
||||
break;
|
||||
case 'jsonp':
|
||||
$response = new Jsonp($result);
|
||||
break;
|
||||
case 'html':
|
||||
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch(Config::get('dispatch_error_tmpl'), $result);
|
||||
default:
|
||||
$response = new Response($result, $type);
|
||||
if ('html' == strtolower($type)) {
|
||||
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch(Config::get('dispatch_error_tmpl'), $result);
|
||||
}
|
||||
$response = Response::create($result, $type);
|
||||
throw new HttpResponseException($response);
|
||||
}
|
||||
|
||||
@@ -119,7 +98,7 @@ trait Jump
|
||||
*/
|
||||
public function result($data, $code = 0, $msg = '', $type = '')
|
||||
{
|
||||
return (new Response([], $type))->result($data, $code, $msg);
|
||||
return Response::create([], $type)->result($data, $code, $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,7 +111,9 @@ trait Jump
|
||||
*/
|
||||
public function redirect($url, $code = 301, $params = [])
|
||||
{
|
||||
return (new Redirect($url))->code($code)->params($params);
|
||||
$response = new Redirect($url);
|
||||
$response->code($code)->params($params);
|
||||
throw new HttpResponseException($response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user