mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-09 08:02:48 +08:00
response类增加create方法 改进think\response\redirect类 改进助手函数和traits\controller\Jump
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user