修正rest控制器一处语法错误

This commit is contained in:
thinkphp
2015-12-02 12:48:50 +08:00
parent f475be3516
commit 0dc82f3ab0

View File

@@ -10,40 +10,44 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\controller; namespace think\controller;
use think\Response; use think\Response;
abstract class rest { abstract class rest
{
protected $_method = ''; // 当前请求类型 protected $_method = ''; // 当前请求类型
protected $_type = ''; // 当前资源类型 protected $_type = ''; // 当前资源类型
// 输出类型 // 输出类型
protected $restMethodList = 'get|post|put|delete'; protected $restMethodList = 'get|post|put|delete';
protected $restDefaultMethod = 'get'; protected $restDefaultMethod = 'get';
protected $restTypeList = 'html|xml|json|rss'; protected $restTypeList = 'html|xml|json|rss';
protected $restDefaultType = 'html'; protected $restDefaultType = 'html';
protected $restOutputType = [ // REST允许输出的资源类型列表 protected $restOutputType = [// REST允许输出的资源类型列表
'xml' => 'application/xml', 'xml' => 'application/xml',
'json' => 'application/json', 'json' => 'application/json',
'html' => 'text/html', 'html' => 'text/html',
]; ];
/** /**
* 架构函数 取得模板对象实例 * 架构函数 取得模板对象实例
* @access public * @access public
*/ */
public function __construct() { public function __construct()
{
// 资源类型检测 // 资源类型检测
if(''==__EXT__) { // 自动检测资源类型 if ('' == __EXT__) {
$this->_type = $this->getAcceptType(); // 自动检测资源类型
}elseif(!preg_match('/\('.$this->restTypeList.')$/i',__EXT__)) { $this->_type = $this->getAcceptType();
} 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($_SERVER['REQUEST_METHOD']); $method = strtolower($_SERVER['REQUEST_METHOD']);
if(false === stripos($this->restMethodList,$method)) { if (false === stripos($this->restMethodList, $method)) {
// 请求方式非法 则用默认请求方法 // 请求方式非法 则用默认请求方法
$method = $this->restDefaultMethod; $method = $this->restDefaultMethod;
} }
@@ -57,19 +61,21 @@ abstract class rest {
* @param array $args 参数 * @param array $args 参数
* @return mixed * @return mixed
*/ */
public function _empty($method,$args) { public function _empty($method, $args)
if(method_exists($this,$method.'_'.$this->_method.'_'.$this->_type)) { // RESTFul方法支持 {
$fun = $method.'_'.$this->_method.'_'.$this->_type; if (method_exists($this, $method . '_' . $this->_method . '_' . $this->_type)) {
}elseif($this->_method == $this->restDefaultMethod && method_exists($this,$method.'_'.$this->_type) ){ // RESTFul方法支持
$fun = $method.'_'.$this->_type; $fun = $method . '_' . $this->_method . '_' . $this->_type;
}elseif($this->_type == $this->restDefaultType && method_exists($this,$method.'_'.$this->_method) ){ } elseif ($this->_method == $this->restDefaultMethod && method_exists($this, $method . '_' . $this->_type)) {
$fun = $method.'_'.$this->_method; $fun = $method . '_' . $this->_type;
} elseif ($this->_type == $this->restDefaultType && method_exists($this, $method . '_' . $this->_method)) {
$fun = $method . '_' . $this->_method;
} }
if(isset($fun)) { if (isset($fun)) {
$this->$fun(); $this->$fun();
}else{ } else {
// 抛出异常 // 抛出异常
throw new \think\Exception(\think\Lang::get('_ERROR_ACTION_:').ACTION_NAME); throw new \think\Exception(\think\Lang::get('_ERROR_ACTION_:') . ACTION_NAME);
} }
} }
@@ -81,37 +87,39 @@ abstract class rest {
* @param integer $code HTTP状态 * @param integer $code HTTP状态
* @return void * @return void
*/ */
protected function response($data,$type='',$code=200) { protected function response($data, $type = '', $code = 200)
{
Response::sendHttpStatus($code); Response::sendHttpStatus($code);
Response::returnData($data,strtolower($type))); Response::returnData($data, strtolower($type));
} }
/** /**
* 获取当前请求的Accept头信息 * 获取当前请求的Accept头信息
* @return string * @return string
*/ */
static public function getAcceptType(){ public static function getAcceptType()
{
$type = [ $type = [
'html' => 'text/html,application/xhtml+xml,*/*', 'html' => 'text/html,application/xhtml+xml,*/*',
'xml' => 'application/xml,text/xml,application/x-xml', 'xml' => 'application/xml,text/xml,application/x-xml',
'json' => 'application/json,text/x-json,application/jsonrequest,text/json', 'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
'js' => 'text/javascript,application/javascript,application/x-javascript', 'js' => 'text/javascript,application/javascript,application/x-javascript',
'css' => 'text/css', 'css' => 'text/css',
'rss' => 'application/rss+xml', 'rss' => 'application/rss+xml',
'yaml' => 'application/x-yaml,text/yaml', 'yaml' => 'application/x-yaml,text/yaml',
'atom' => 'application/atom+xml', 'atom' => 'application/atom+xml',
'pdf' => 'application/pdf', 'pdf' => 'application/pdf',
'text' => 'text/plain', 'text' => 'text/plain',
'png' => 'image/png', 'png' => 'image/png',
'jpg' => 'image/jpg,image/jpeg,image/pjpeg', 'jpg' => 'image/jpg,image/jpeg,image/pjpeg',
'gif' => 'image/gif', 'gif' => 'image/gif',
'csv' => 'text/csv' 'csv' => 'text/csv',
]; ];
foreach($type as $key=>$val){ foreach ($type as $key => $val) {
$array = explode(',',$val); $array = explode(',', $val);
foreach($array as $k=>$v){ foreach ($array as $k => $v) {
if(stristr($_SERVER['HTTP_ACCEPT'], $v)) { if (stristr($_SERVER['HTTP_ACCEPT'], $v)) {
return $key; return $key;
} }
} }