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

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

View File

@@ -10,9 +10,11 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
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 = ''; // 当前资源类型
@@ -31,9 +33,11 @@ abstract class rest {
* 架构函数 取得模板对象实例 * 架构函数 取得模板对象实例
* @access public * @access public
*/ */
public function __construct() { public function __construct()
{
// 资源类型检测 // 资源类型检测
if(''==__EXT__) { // 自动检测资源类型 if ('' == __EXT__) {
// 自动检测资源类型
$this->_type = $this->getAcceptType(); $this->_type = $this->getAcceptType();
} elseif (!preg_match('/\(' . $this->restTypeList . ')$/i', __EXT__)) { } elseif (!preg_match('/\(' . $this->restTypeList . ')$/i', __EXT__)) {
// 资源类型非法 则用默认资源类型访问 // 资源类型非法 则用默认资源类型访问
@@ -57,8 +61,10 @@ 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方法支持 {
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->restDefaultMethod && 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;
@@ -81,16 +87,18 @@ 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',
@@ -105,7 +113,7 @@ abstract class rest {
'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) {