改进Response类的send方法 去除 REQUEST_METHOD IS_GET IS_POST IS_PUT IS_DELETE IS_AJAX __EXT__ 常量 由应用自己定义

This commit is contained in:
thinkphp
2016-06-02 12:13:00 +08:00
parent ee9d5df56f
commit 8099a001e2
7 changed files with 63 additions and 32 deletions

View File

@@ -32,22 +32,14 @@ class App
/**
* 执行应用程序
* @access public
* @param null|\think\Request $request Request对象
* @param Request $request Request对象
* @return mixed
* @throws Exception
*/
public static function run($request = null)
public static function run(Request $request = null)
{
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());
@@ -133,7 +125,9 @@ class App
if ($data instanceof Response) {
return $data->send();
} 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();
}
}

View File

@@ -11,6 +11,9 @@
namespace think;
use think\Hook;
use think\Request;
class Response
{
// 输出类型的实例化对象
@@ -48,16 +51,20 @@ class Response
public function __construct($data = [], $type = '', $options = [])
{
$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])) {
$this->contentType($this->contentTypes[$this->type]);
}
if(!empty($options)){
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
// 方便获取某个类型的实例
self::$instance[$this->type] = $this;
self::$instance[$this->type] = $this;
}
/**
@@ -70,7 +77,12 @@ class Response
*/
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])) {
$class = (isset($options['namespace']) ? $options['namespace'] : '\\think\\response\\') . ucfirst($type);
if (class_exists($class)) {
@@ -90,9 +102,9 @@ class Response
* @return mixed
* @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)) {
$this->contentType($this->contentType);
@@ -102,7 +114,7 @@ class Response
$data = call_user_func_array($this->transform, [$data]);
}
defined('RESPONSE_TYPE') or define('RESPONSE_TYPE',$this->type);
defined('RESPONSE_TYPE') or define('RESPONSE_TYPE', $this->type);
// 处理输出数据
$data = $this->output($data);

View File

@@ -11,6 +11,12 @@
namespace think;
use think\Config;
use think\Hook;
use think\Log;
use think\Request;
use think\Response;
class Route
{
// 路由规则
@@ -600,7 +606,7 @@ class Route
}
// 获取当前请求类型的路由规则
$rules = self::$rules[REQUEST_METHOD];
$rules = self::$rules[$request->method()];
if (!empty(self::$rules['*'])) {
// 合并任意请求的路由规则
@@ -747,8 +753,8 @@ class Route
private static function checkOption($option, $url, $request)
{
// 请求类型检测
if ((isset($option['method']) && false === stripos($option['method'], REQUEST_METHOD))
|| (isset($option['ext']) && false === stripos($option['ext'], __EXT__)) // 伪静态后缀检测
if ((isset($option['method']) && false === stripos($option['method'], $request->method()))
|| (isset($option['ext']) && false === stripos($option['ext'], $request->ext())) // 伪静态后缀检测
|| (isset($option['domain']) && !in_array($option['domain'], [$_SERVER['HTTP_HOST'], self::$subDomain])) // 域名检测
|| (!empty($option['https']) && !$request->isSsl()) // https检测
|| (!empty($option['before_behavior']) && false === Hook::exec($option['before_behavior'], $url)) // 行为检测
@@ -915,12 +921,13 @@ class Route
$action = array_pop($path);
$controller = !empty($path) ? array_pop($path) : null;
$module = APP_MULTI_MODULE && !empty($path) ? array_pop($path) : null;
$method = Request::instance()->method();
// REST 操作方法支持
if ('[rest]' == $action) {
$action = REQUEST_METHOD;
} elseif (Config::get('use_action_prefix') && !empty(self::$methodPrefix[REQUEST_METHOD])) {
$action = $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;
}
}
// 封装路由

View File

@@ -12,6 +12,7 @@
namespace think;
use think\Input;
use think\Request;
class Validate
{
@@ -684,7 +685,8 @@ class Validate
*/
protected function method($value, $rule)
{
return REQUEST_METHOD == strtoupper($rule);
$method = Request::instance()->method();
return $method == strtoupper($rule);
}
/**

View File

@@ -12,6 +12,7 @@
namespace think\controller;
use think\Response;
use think\Request;
abstract class Rest
{
@@ -36,17 +37,19 @@ abstract class Rest
public function __construct()
{
// 资源类型检测
if ('' == __EXT__) {
$request = Request::instance();
$ext = $request->ext();
if ('' == $ext) {
// 自动检测资源类型
$this->_type = $this->getAcceptType();
} elseif (!preg_match('/\(' . $this->restTypeList . '\)$/i', __EXT__)) {
} elseif (!preg_match('/\(' . $this->restTypeList . '\)$/i', $ext)) {
// 资源类型非法 则用默认资源类型访问
$this->_type = $this->restDefaultType;
} else {
$this->_type = __EXT__;
$this->_type = $ext;
}
// 请求方式检测
$method = strtolower(REQUEST_METHOD);
$method = strtolower($request->method());
if (false === stripos($this->restMethodList, $method)) {
// 请求方式非法 则用默认请求方法
$method = $this->restDefaultMethod;

View File

@@ -15,6 +15,7 @@ use think\Cache;
use think\Config;
use think\Db;
use think\Debug;
use think\Request;
/**
* 页面Trace调试
@@ -41,10 +42,11 @@ class Trace
*/
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方式下不输出
return false;
}
// 获取基本信息
$runtime = microtime(true) - START_TIME;
$reqs = number_format(1 / number_format($runtime, 8), 2);

View File

@@ -16,6 +16,7 @@ namespace traits\controller;
use think\Config;
use think\exception\HttpResponseException;
use think\Request;
use think\Response;
use think\response\Redirect;
use think\View as ViewTemplate;
@@ -46,7 +47,7 @@ trait Jump
'wait' => $wait,
];
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
$type = $this->getResponseType();
if ('html' == strtolower($type)) {
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
->fetch(Config::get('dispatch_success_tmpl'), $result);
@@ -78,7 +79,7 @@ trait Jump
'wait' => $wait,
];
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
$type = $this->getResponseType();
if ('html' == strtolower($type)) {
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
->fetch(Config::get('dispatch_error_tmpl'), $result);
@@ -120,4 +121,14 @@ trait Jump
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');
}
}