Think\Rest类移动到Think\Controller\Rest

This commit is contained in:
thinkphp
2013-04-10 10:09:37 +08:00
parent 15a45e9ff7
commit d81277ba19

View File

@@ -9,20 +9,20 @@
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace Think; namespace Think\Controller;
abstract class Rest { abstract class Rest {
protected $_method = ''; // 当前请求类型 protected $_method = ''; // 当前请求类型
protected $_type = ''; // 当前资源类型 protected $_type = ''; // 当前资源类型
// 输出类型 // 输出类型
protected $rest_method_list = 'get,post,put,delete'; protected $restMethodList = 'get|post|put|delete';
protected $rest_default_method = 'get'; protected $restDefaultMethod = 'get';
protected $rest_type_list = 'html,xml,json,rss'; protected $restTypeList = 'html|xml|json|rss';
protected $rest_default_type = 'html'; protected $restDefaultType = 'html';
protected $rest_output_type = [ // REST允许输出的资源类型列表 protected $restOutputType = [ // REST允许输出的资源类型列表
'xml' => 'application/xml', 'xml' => 'application/xml',
'json' => 'application/json', 'json' => 'application/json',
'html' => 'text/html', 'html' => 'text/html',
]; ];
/** /**
@@ -31,19 +31,19 @@ abstract class Rest {
*/ */
public function __construct() { public function __construct() {
// 资源类型检测 // 资源类型检测
if(!defined('__EXT__') || ''==__EXT__) { // 自动检测资源类型 if(''==__EXT__) { // 自动检测资源类型
$this->_type = $this->getAcceptType(); $this->_type = $this->getAcceptType();
}elseif(false === stripos($this->rest_type_list,__EXT__)) { }elseif(!preg_match('/\('.$this->restTypeList.')$/i',__EXT__)) {
// 资源类型非法 则用默认资源类型访问 // 资源类型非法 则用默认资源类型访问
$this->_type = $this->rest_default_type; $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->rest_method_list,$method)) { if(false === stripos($this->restMethodList,$method)) {
// 请求方式非法 则用默认请求方法 // 请求方式非法 则用默认请求方法
$method = $this->rest_default_method; $method = $this->restDefaultMethod;
} }
$this->_method = $method; $this->_method = $method;
} }
@@ -58,9 +58,9 @@ abstract class Rest {
public function _empty($method,$args) { public function _empty($method,$args) {
if(method_exists($this,$method.'_'.$this->_method.'_'.$this->_type)) { // RESTFul方法支持 if(method_exists($this,$method.'_'.$this->_method.'_'.$this->_type)) { // RESTFul方法支持
$fun = $method.'_'.$this->_method.'_'.$this->_type; $fun = $method.'_'.$this->_method.'_'.$this->_type;
}elseif($this->_method == $this->rest_default_method && method_exists($this,$method.'_'.$this->_type) ){ }elseif($this->_method == $this->restDefaultMethod && method_exists($this,$method.'_'.$this->_type) ){
$fun = $method.'_'.$this->_type; $fun = $method.'_'.$this->_type;
}elseif($this->_type == $this->rest_default_type && method_exists($this,$method.'_'.$this->_method) ){ }elseif($this->_type == $this->restDefaultType && method_exists($this,$method.'_'.$this->_method) ){
$fun = $method.'_'.$this->_method; $fun = $method.'_'.$this->_method;
} }
if(isset($fun)) { if(isset($fun)) {
@@ -78,12 +78,11 @@ abstract class Rest {
* @param string $charset 页面输出编码 * @param string $charset 页面输出编码
* @return void * @return void
*/ */
public function setContentType($type, $charset=''){ public function setContentType($type, $charset='utf-8'){
if(headers_sent()) return; if(headers_sent()) return;
if(empty($charset)) $charset = ThinkConfig::get('default_charset');
$type = strtolower($type); $type = strtolower($type);
if(isset($this->rest_output_type[$type])) //过滤content_type if(isset($this->restOutputType[$type])) //过滤content_type
header('Content-Type: '.$this->rest_output_type[$type].'; charset='.$charset); header('Content-Type: '.$this->restOutputType[$type].'; charset='.$charset);
} }
/** /**