mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-05 22:52:49 +08:00
注释规范
This commit is contained in:
@@ -17,8 +17,8 @@ use think\Request;
|
||||
abstract class Rest
|
||||
{
|
||||
|
||||
protected $_method = ''; // 当前请求类型
|
||||
protected $_type = ''; // 当前资源类型
|
||||
protected $method; // 当前请求类型
|
||||
protected $type; // 当前资源类型
|
||||
// 输出类型
|
||||
protected $restMethodList = 'get|post|put|delete';
|
||||
protected $restDefaultMethod = 'get';
|
||||
@@ -41,12 +41,12 @@ abstract class Rest
|
||||
$ext = $request->ext();
|
||||
if ('' == $ext) {
|
||||
// 自动检测资源类型
|
||||
$this->_type = $this->getAcceptType();
|
||||
$this->type = $request->type();
|
||||
} elseif (!preg_match('/\(' . $this->restTypeList . '\)$/i', $ext)) {
|
||||
// 资源类型非法 则用默认资源类型访问
|
||||
$this->_type = $this->restDefaultType;
|
||||
$this->type = $this->restDefaultType;
|
||||
} else {
|
||||
$this->_type = $ext;
|
||||
$this->type = $ext;
|
||||
}
|
||||
// 请求方式检测
|
||||
$method = strtolower($request->method());
|
||||
@@ -54,7 +54,7 @@ abstract class Rest
|
||||
// 请求方式非法 则用默认请求方法
|
||||
$method = $this->restDefaultMethod;
|
||||
}
|
||||
$this->_method = $method;
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,13 +67,13 @@ abstract class Rest
|
||||
*/
|
||||
public function _empty($method, $args)
|
||||
{
|
||||
if (method_exists($this, $method . '_' . $this->_method . '_' . $this->_type)) {
|
||||
if (method_exists($this, $method . '_' . $this->method . '_' . $this->type)) {
|
||||
// RESTFul方法支持
|
||||
$fun = $method . '_' . $this->_method . '_' . $this->_type;
|
||||
} elseif ($this->_method == $this->restDefaultMethod && method_exists($this, $method . '_' . $this->_type)) {
|
||||
$fun = $method . '_' . $this->_type;
|
||||
} elseif ($this->_type == $this->restDefaultType && method_exists($this, $method . '_' . $this->_method)) {
|
||||
$fun = $method . '_' . $this->_method;
|
||||
$fun = $method . '_' . $this->method . '_' . $this->type;
|
||||
} elseif ($this->_method == $this->restDefaultMethod && method_exists($this, $method . '_' . $this->type)) {
|
||||
$fun = $method . '_' . $this->type;
|
||||
} elseif ($this->type == $this->restDefaultType && method_exists($this, $method . '_' . $this->method)) {
|
||||
$fun = $method . '_' . $this->method;
|
||||
}
|
||||
if (isset($fun)) {
|
||||
return $this->$fun();
|
||||
@@ -86,9 +86,9 @@ abstract class Rest
|
||||
/**
|
||||
* 输出返回数据
|
||||
* @access protected
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param String $type 返回类型 JSON XML
|
||||
* @param integer $code HTTP状态
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param String $type 返回类型 JSON XML
|
||||
* @param integer $code HTTP状态码
|
||||
* @return Response
|
||||
*/
|
||||
protected function response($data, $type = 'json', $code = 200)
|
||||
@@ -96,42 +96,4 @@ abstract class Rest
|
||||
return Response::create($data, $type)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前请求的Accept头信息
|
||||
* @return string
|
||||
*/
|
||||
public static function getAcceptType()
|
||||
{
|
||||
if (!isset($_SERVER['HTTP_ACCEPT'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$type = [
|
||||
'html' => 'text/html,application/xhtml+xml,*/*',
|
||||
'xml' => 'application/xml,text/xml,application/x-xml',
|
||||
'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
|
||||
'js' => 'text/javascript,application/javascript,application/x-javascript',
|
||||
'css' => 'text/css',
|
||||
'rss' => 'application/rss+xml',
|
||||
'yaml' => 'application/x-yaml,text/yaml',
|
||||
'atom' => 'application/atom+xml',
|
||||
'pdf' => 'application/pdf',
|
||||
'text' => 'text/plain',
|
||||
'png' => 'image/png',
|
||||
'jpg' => 'image/jpg,image/jpeg,image/pjpeg',
|
||||
'gif' => 'image/gif',
|
||||
'csv' => 'text/csv',
|
||||
];
|
||||
|
||||
foreach ($type as $key => $val) {
|
||||
$array = explode(',', $val);
|
||||
foreach ($array as $k => $v) {
|
||||
if (stristr($_SERVER['HTTP_ACCEPT'], $v)) {
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user