Rest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\controller;
  12. use think\App;
  13. use think\Request;
  14. use think\Response;
  15. abstract class Rest
  16. {
  17. protected $method; // 当前请求类型
  18. protected $type; // 当前资源类型
  19. // 输出类型
  20. protected $restMethodList = 'get|post|put|delete';
  21. protected $restDefaultMethod = 'get';
  22. protected $restTypeList = 'html|xml|json|rss';
  23. protected $restDefaultType = 'html';
  24. protected $restOutputType = [ // REST允许输出的资源类型列表
  25. 'xml' => 'application/xml',
  26. 'json' => 'application/json',
  27. 'html' => 'text/html',
  28. ];
  29. /**
  30. * 构造函数 取得模板对象实例
  31. * @access public
  32. */
  33. public function __construct()
  34. {
  35. // 资源类型检测
  36. $request = Request::instance();
  37. $ext = $request->ext();
  38. if ('' == $ext) {
  39. // 自动检测资源类型
  40. $this->type = $request->type();
  41. } elseif (!preg_match('/(' . $this->restTypeList . ')$/i', $ext)) {
  42. // 资源类型非法 则用默认资源类型访问
  43. $this->type = $this->restDefaultType;
  44. } else {
  45. $this->type = $ext;
  46. }
  47. // 请求方式检测
  48. $method = strtolower($request->method());
  49. if (!preg_match('/(' . $this->restMethodList . ')$/i', $method)) {
  50. // 请求方式非法 则用默认请求方法
  51. $method = $this->restDefaultMethod;
  52. }
  53. $this->method = $method;
  54. }
  55. /**
  56. * REST 调用
  57. * @access public
  58. * @param string $method 方法名
  59. * @return mixed
  60. * @throws \Exception
  61. */
  62. public function _empty($method)
  63. {
  64. if (method_exists($this, $method . '_' . $this->method . '_' . $this->type)) {
  65. // RESTFul方法支持
  66. $fun = $method . '_' . $this->method . '_' . $this->type;
  67. } elseif ($this->method == $this->restDefaultMethod && method_exists($this, $method . '_' . $this->type)) {
  68. $fun = $method . '_' . $this->type;
  69. } elseif ($this->type == $this->restDefaultType && method_exists($this, $method . '_' . $this->method)) {
  70. $fun = $method . '_' . $this->method;
  71. }
  72. if (isset($fun)) {
  73. return App::invokeMethod([$this, $fun]);
  74. } else {
  75. // 抛出异常
  76. throw new \Exception('error action :' . $method);
  77. }
  78. }
  79. /**
  80. * 输出返回数据
  81. * @access protected
  82. * @param mixed $data 要返回的数据
  83. * @param String $type 返回类型 JSON XML
  84. * @param integer $code HTTP状态码
  85. * @return Response
  86. */
  87. protected function response($data, $type = 'json', $code = 200)
  88. {
  89. return Response::create($data, $type)->code($code);
  90. }
  91. }