mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
改进Response类的send方法 去除 REQUEST_METHOD IS_GET IS_POST IS_PUT IS_DELETE IS_AJAX __EXT__ 常量 由应用自己定义
This commit is contained in:
@@ -32,22 +32,14 @@ class App
|
|||||||
/**
|
/**
|
||||||
* 执行应用程序
|
* 执行应用程序
|
||||||
* @access public
|
* @access public
|
||||||
* @param null|\think\Request $request Request对象
|
* @param Request $request Request对象
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function run($request = null)
|
public static function run(Request $request = null)
|
||||||
{
|
{
|
||||||
is_null($request) && $request = Request::instance();
|
is_null($request) && $request = Request::instance();
|
||||||
|
|
||||||
define('REQUEST_METHOD', $request->method());
|
|
||||||
define('IS_GET', REQUEST_METHOD == 'GET' ? true : false);
|
|
||||||
define('IS_POST', REQUEST_METHOD == 'POST' ? true : false);
|
|
||||||
define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false);
|
|
||||||
define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false);
|
|
||||||
define('IS_AJAX', $request->isAjax());
|
|
||||||
define('__EXT__', $request->ext());
|
|
||||||
|
|
||||||
// 初始化应用
|
// 初始化应用
|
||||||
$config = self::init('', Config::get());
|
$config = self::init('', Config::get());
|
||||||
|
|
||||||
@@ -133,7 +125,9 @@ class App
|
|||||||
if ($data instanceof Response) {
|
if ($data instanceof Response) {
|
||||||
return $data->send();
|
return $data->send();
|
||||||
} elseif (!is_null($data)) {
|
} elseif (!is_null($data)) {
|
||||||
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
// 默认自动识别响应输出类型
|
||||||
|
$isAjax = $request->isAjax();
|
||||||
|
$type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
||||||
return Response::create($data, $type)->send();
|
return Response::create($data, $type)->send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,9 @@
|
|||||||
|
|
||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
|
use think\Hook;
|
||||||
|
use think\Request;
|
||||||
|
|
||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
// 输出类型的实例化对象
|
// 输出类型的实例化对象
|
||||||
@@ -48,7 +51,11 @@ class Response
|
|||||||
public function __construct($data = [], $type = '', $options = [])
|
public function __construct($data = [], $type = '', $options = [])
|
||||||
{
|
{
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
$this->type = strtolower($type ?: (IS_AJAX ? 'json' : 'html'));
|
if (empty($type)) {
|
||||||
|
$isAjax = Request::instance()->isAjax();
|
||||||
|
$type = $isAjax ? 'json' : 'html';
|
||||||
|
}
|
||||||
|
$this->type = strtolower($type);
|
||||||
|
|
||||||
if (isset($this->contentTypes[$this->type])) {
|
if (isset($this->contentTypes[$this->type])) {
|
||||||
$this->contentType($this->contentTypes[$this->type]);
|
$this->contentType($this->contentTypes[$this->type]);
|
||||||
@@ -70,7 +77,12 @@ class Response
|
|||||||
*/
|
*/
|
||||||
public static function create($data = [], $type = '', $options = [])
|
public static function create($data = [], $type = '', $options = [])
|
||||||
{
|
{
|
||||||
$type = strtolower($type ?: (IS_AJAX ? 'json' : 'html'));
|
if (empty($type)) {
|
||||||
|
$isAjax = Request::instance()->isAjax();
|
||||||
|
$type = $isAjax ? 'json' : 'html';
|
||||||
|
}
|
||||||
|
$type = strtolower($type);
|
||||||
|
|
||||||
if (!isset(self::$instance[$type])) {
|
if (!isset(self::$instance[$type])) {
|
||||||
$class = (isset($options['namespace']) ? $options['namespace'] : '\\think\\response\\') . ucfirst($type);
|
$class = (isset($options['namespace']) ? $options['namespace'] : '\\think\\response\\') . ucfirst($type);
|
||||||
if (class_exists($class)) {
|
if (class_exists($class)) {
|
||||||
@@ -90,9 +102,9 @@ class Response
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function send($data = [])
|
public function send($data = null)
|
||||||
{
|
{
|
||||||
$data = $data ?: $this->data;
|
$data = !is_null($data) ?: $this->data;
|
||||||
|
|
||||||
if (isset($this->contentType)) {
|
if (isset($this->contentType)) {
|
||||||
$this->contentType($this->contentType);
|
$this->contentType($this->contentType);
|
||||||
|
|||||||
@@ -11,6 +11,12 @@
|
|||||||
|
|
||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
|
use think\Config;
|
||||||
|
use think\Hook;
|
||||||
|
use think\Log;
|
||||||
|
use think\Request;
|
||||||
|
use think\Response;
|
||||||
|
|
||||||
class Route
|
class Route
|
||||||
{
|
{
|
||||||
// 路由规则
|
// 路由规则
|
||||||
@@ -600,7 +606,7 @@ class Route
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前请求类型的路由规则
|
// 获取当前请求类型的路由规则
|
||||||
$rules = self::$rules[REQUEST_METHOD];
|
$rules = self::$rules[$request->method()];
|
||||||
|
|
||||||
if (!empty(self::$rules['*'])) {
|
if (!empty(self::$rules['*'])) {
|
||||||
// 合并任意请求的路由规则
|
// 合并任意请求的路由规则
|
||||||
@@ -747,8 +753,8 @@ class Route
|
|||||||
private static function checkOption($option, $url, $request)
|
private static function checkOption($option, $url, $request)
|
||||||
{
|
{
|
||||||
// 请求类型检测
|
// 请求类型检测
|
||||||
if ((isset($option['method']) && false === stripos($option['method'], REQUEST_METHOD))
|
if ((isset($option['method']) && false === stripos($option['method'], $request->method()))
|
||||||
|| (isset($option['ext']) && false === stripos($option['ext'], __EXT__)) // 伪静态后缀检测
|
|| (isset($option['ext']) && false === stripos($option['ext'], $request->ext())) // 伪静态后缀检测
|
||||||
|| (isset($option['domain']) && !in_array($option['domain'], [$_SERVER['HTTP_HOST'], self::$subDomain])) // 域名检测
|
|| (isset($option['domain']) && !in_array($option['domain'], [$_SERVER['HTTP_HOST'], self::$subDomain])) // 域名检测
|
||||||
|| (!empty($option['https']) && !$request->isSsl()) // https检测
|
|| (!empty($option['https']) && !$request->isSsl()) // https检测
|
||||||
|| (!empty($option['before_behavior']) && false === Hook::exec($option['before_behavior'], $url)) // 行为检测
|
|| (!empty($option['before_behavior']) && false === Hook::exec($option['before_behavior'], $url)) // 行为检测
|
||||||
@@ -915,12 +921,13 @@ class Route
|
|||||||
$action = array_pop($path);
|
$action = array_pop($path);
|
||||||
$controller = !empty($path) ? array_pop($path) : null;
|
$controller = !empty($path) ? array_pop($path) : null;
|
||||||
$module = APP_MULTI_MODULE && !empty($path) ? array_pop($path) : null;
|
$module = APP_MULTI_MODULE && !empty($path) ? array_pop($path) : null;
|
||||||
|
$method = Request::instance()->method();
|
||||||
// REST 操作方法支持
|
// REST 操作方法支持
|
||||||
if ('[rest]' == $action) {
|
if ('[rest]' == $action) {
|
||||||
$action = REQUEST_METHOD;
|
$action = $method;
|
||||||
} elseif (Config::get('use_action_prefix') && !empty(self::$methodPrefix[REQUEST_METHOD])) {
|
} elseif (Config::get('use_action_prefix') && !empty(self::$methodPrefix[$method])) {
|
||||||
// 操作方法前缀支持
|
// 操作方法前缀支持
|
||||||
$action = 0 !== strpos($action, self::$methodPrefix[REQUEST_METHOD]) ? self::$methodPrefix[REQUEST_METHOD] . $action : $action;
|
$action = 0 !== strpos($action, self::$methodPrefix[$method]) ? self::$methodPrefix[$method] . $action : $action;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 封装路由
|
// 封装路由
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
use think\Input;
|
use think\Input;
|
||||||
|
use think\Request;
|
||||||
|
|
||||||
class Validate
|
class Validate
|
||||||
{
|
{
|
||||||
@@ -684,7 +685,8 @@ class Validate
|
|||||||
*/
|
*/
|
||||||
protected function method($value, $rule)
|
protected function method($value, $rule)
|
||||||
{
|
{
|
||||||
return REQUEST_METHOD == strtoupper($rule);
|
$method = Request::instance()->method();
|
||||||
|
return $method == strtoupper($rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
namespace think\controller;
|
namespace think\controller;
|
||||||
|
|
||||||
use think\Response;
|
use think\Response;
|
||||||
|
use think\Request;
|
||||||
|
|
||||||
abstract class Rest
|
abstract class Rest
|
||||||
{
|
{
|
||||||
@@ -36,17 +37,19 @@ abstract class Rest
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
// 资源类型检测
|
// 资源类型检测
|
||||||
if ('' == __EXT__) {
|
$request = Request::instance();
|
||||||
|
$ext = $request->ext();
|
||||||
|
if ('' == $ext) {
|
||||||
// 自动检测资源类型
|
// 自动检测资源类型
|
||||||
$this->_type = $this->getAcceptType();
|
$this->_type = $this->getAcceptType();
|
||||||
} elseif (!preg_match('/\(' . $this->restTypeList . '\)$/i', __EXT__)) {
|
} elseif (!preg_match('/\(' . $this->restTypeList . '\)$/i', $ext)) {
|
||||||
// 资源类型非法 则用默认资源类型访问
|
// 资源类型非法 则用默认资源类型访问
|
||||||
$this->_type = $this->restDefaultType;
|
$this->_type = $this->restDefaultType;
|
||||||
} else {
|
} else {
|
||||||
$this->_type = __EXT__;
|
$this->_type = $ext;
|
||||||
}
|
}
|
||||||
// 请求方式检测
|
// 请求方式检测
|
||||||
$method = strtolower(REQUEST_METHOD);
|
$method = strtolower($request->method());
|
||||||
if (false === stripos($this->restMethodList, $method)) {
|
if (false === stripos($this->restMethodList, $method)) {
|
||||||
// 请求方式非法 则用默认请求方法
|
// 请求方式非法 则用默认请求方法
|
||||||
$method = $this->restDefaultMethod;
|
$method = $this->restDefaultMethod;
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ use think\Cache;
|
|||||||
use think\Config;
|
use think\Config;
|
||||||
use think\Db;
|
use think\Db;
|
||||||
use think\Debug;
|
use think\Debug;
|
||||||
|
use think\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 页面Trace调试
|
* 页面Trace调试
|
||||||
@@ -41,10 +42,11 @@ class Trace
|
|||||||
*/
|
*/
|
||||||
public function save(array $log = [])
|
public function save(array $log = [])
|
||||||
{
|
{
|
||||||
if (IS_CLI || IS_AJAX || IS_API || (defined('RESPONSE_TYPE') && !in_array(RESPONSE_TYPE, ['html', 'view']))) {
|
if (IS_CLI || IS_API || Request::instance()->isAjax() || (defined('RESPONSE_TYPE') && !in_array(RESPONSE_TYPE, ['html', 'view']))) {
|
||||||
// ajax cli api方式下不输出
|
// ajax cli api方式下不输出
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取基本信息
|
// 获取基本信息
|
||||||
$runtime = microtime(true) - START_TIME;
|
$runtime = microtime(true) - START_TIME;
|
||||||
$reqs = number_format(1 / number_format($runtime, 8), 2);
|
$reqs = number_format(1 / number_format($runtime, 8), 2);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace traits\controller;
|
|||||||
|
|
||||||
use think\Config;
|
use think\Config;
|
||||||
use think\exception\HttpResponseException;
|
use think\exception\HttpResponseException;
|
||||||
|
use think\Request;
|
||||||
use think\Response;
|
use think\Response;
|
||||||
use think\response\Redirect;
|
use think\response\Redirect;
|
||||||
use think\View as ViewTemplate;
|
use think\View as ViewTemplate;
|
||||||
@@ -46,7 +47,7 @@ trait Jump
|
|||||||
'wait' => $wait,
|
'wait' => $wait,
|
||||||
];
|
];
|
||||||
|
|
||||||
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
$type = $this->getResponseType();
|
||||||
if ('html' == strtolower($type)) {
|
if ('html' == strtolower($type)) {
|
||||||
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||||
->fetch(Config::get('dispatch_success_tmpl'), $result);
|
->fetch(Config::get('dispatch_success_tmpl'), $result);
|
||||||
@@ -78,7 +79,7 @@ trait Jump
|
|||||||
'wait' => $wait,
|
'wait' => $wait,
|
||||||
];
|
];
|
||||||
|
|
||||||
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
$type = $this->getResponseType();
|
||||||
if ('html' == strtolower($type)) {
|
if ('html' == strtolower($type)) {
|
||||||
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||||
->fetch(Config::get('dispatch_error_tmpl'), $result);
|
->fetch(Config::get('dispatch_error_tmpl'), $result);
|
||||||
@@ -120,4 +121,14 @@ trait Jump
|
|||||||
throw new HttpResponseException($response);
|
throw new HttpResponseException($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前的response 输出类型
|
||||||
|
* @access public
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getResponseType()
|
||||||
|
{
|
||||||
|
$isAjax = Request::instance()->isAjax();
|
||||||
|
return $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user