修正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;
use think\Response;
abstract class rest {
abstract class rest
{
protected $_method = ''; // 当前请求类型
protected $_type = ''; // 当前资源类型
protected $_method = ''; // 当前请求类型
protected $_type = ''; // 当前资源类型
// 输出类型
protected $restMethodList = 'get|post|put|delete';
protected $restDefaultMethod = 'get';
protected $restTypeList = 'html|xml|json|rss';
protected $restDefaultType = 'html';
protected $restOutputType = [ // REST允许输出的资源类型列表
'xml' => 'application/xml',
'json' => 'application/json',
'html' => 'text/html',
];
/**
protected $restMethodList = 'get|post|put|delete';
protected $restDefaultMethod = 'get';
protected $restTypeList = 'html|xml|json|rss';
protected $restDefaultType = 'html';
protected $restOutputType = [// REST允许输出的资源类型列表
'xml' => 'application/xml',
'json' => 'application/json',
'html' => 'text/html',
];
/**
* 架构函数 取得模板对象实例
* @access public
*/
public function __construct() {
public function __construct()
{
// 资源类型检测
if(''==__EXT__) { // 自动检测资源类型
$this->_type = $this->getAcceptType();
}elseif(!preg_match('/\('.$this->restTypeList.')$/i',__EXT__)) {
if ('' == __EXT__) {
// 自动检测资源类型
$this->_type = $this->getAcceptType();
} elseif (!preg_match('/\(' . $this->restTypeList . ')$/i', __EXT__)) {
// 资源类型非法 则用默认资源类型访问
$this->_type = $this->restDefaultType;
}else{
$this->_type = __EXT__;
$this->_type = $this->restDefaultType;
} else {
$this->_type = __EXT__;
}
// 请求方式检测
$method = strtolower($_SERVER['REQUEST_METHOD']);
if(false === stripos($this->restMethodList,$method)) {
$method = strtolower($_SERVER['REQUEST_METHOD']);
if (false === stripos($this->restMethodList, $method)) {
// 请求方式非法 则用默认请求方法
$method = $this->restDefaultMethod;
}
@@ -57,19 +61,21 @@ abstract class rest {
* @param array $args 参数
* @return mixed
*/
public function _empty($method,$args) {
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;
public function _empty($method, $args)
{
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;
}
if(isset($fun)) {
if (isset($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状态
* @return void
*/
protected function response($data,$type='',$code=200) {
protected function response($data, $type = '', $code = 200)
{
Response::sendHttpStatus($code);
Response::returnData($data,strtolower($type)));
Response::returnData($data, strtolower($type));
}
/**
* 获取当前请求的Accept头信息
* @return string
*/
static public function getAcceptType(){
public static function getAcceptType()
{
$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'
'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)) {
foreach ($type as $key => $val) {
$array = explode(',', $val);
foreach ($array as $k => $v) {
if (stristr($_SERVER['HTTP_ACCEPT'], $v)) {
return $key;
}
}