Controller.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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;
  12. use think\exception\ValidateException;
  13. use traits\controller\Jump;
  14. class Controller
  15. {
  16. use Jump;
  17. /**
  18. * 视图类实例
  19. * @var \think\View
  20. */
  21. protected $view;
  22. /**
  23. * Request实例
  24. * @var \think\Request
  25. */
  26. protected $request;
  27. /**
  28. * 验证失败是否抛出异常
  29. * @var bool
  30. */
  31. protected $failException = false;
  32. /**
  33. * 是否批量验证
  34. * @var bool
  35. */
  36. protected $batchValidate = false;
  37. /**
  38. * 前置操作方法列表(即将废弃)
  39. * @var array $beforeActionList
  40. */
  41. protected $beforeActionList = [];
  42. /**
  43. * 控制器中间件
  44. * @var array
  45. */
  46. protected $middleware = [];
  47. /**
  48. * 构造方法
  49. * @access public
  50. */
  51. public function __construct(App $app = null)
  52. {
  53. $this->app = $app ?: Container::get('app');
  54. $this->request = $this->app['request'];
  55. $this->view = $this->app['view'];
  56. // 控制器初始化
  57. $this->initialize();
  58. $this->registerMiddleware();
  59. // 前置操作方法 即将废弃
  60. foreach ((array) $this->beforeActionList as $method => $options) {
  61. is_numeric($method) ?
  62. $this->beforeAction($options) :
  63. $this->beforeAction($method, $options);
  64. }
  65. }
  66. // 初始化
  67. protected function initialize()
  68. {}
  69. // 注册控制器中间件
  70. public function registerMiddleware()
  71. {
  72. foreach ($this->middleware as $key => $val) {
  73. if (!is_int($key)) {
  74. $only = $except = null;
  75. if (isset($val['only'])) {
  76. $only = array_map(function ($item) {
  77. return strtolower($item);
  78. }, $val['only']);
  79. } elseif (isset($val['except'])) {
  80. $except = array_map(function ($item) {
  81. return strtolower($item);
  82. }, $val['except']);
  83. }
  84. if (isset($only) && !in_array($this->request->action(), $only)) {
  85. continue;
  86. } elseif (isset($except) && in_array($this->request->action(), $except)) {
  87. continue;
  88. } else {
  89. $val = $key;
  90. }
  91. }
  92. $this->app['middleware']->controller($val);
  93. }
  94. }
  95. /**
  96. * 前置操作
  97. * @access protected
  98. * @param string $method 前置操作方法名
  99. * @param array $options 调用参数 ['only'=>[...]] 或者['except'=>[...]]
  100. */
  101. protected function beforeAction($method, $options = [])
  102. {
  103. if (isset($options['only'])) {
  104. if (is_string($options['only'])) {
  105. $options['only'] = explode(',', $options['only']);
  106. }
  107. $only = array_map(function ($val) {
  108. return strtolower($val);
  109. }, $options['only']);
  110. if (!in_array($this->request->action(), $only)) {
  111. return;
  112. }
  113. } elseif (isset($options['except'])) {
  114. if (is_string($options['except'])) {
  115. $options['except'] = explode(',', $options['except']);
  116. }
  117. $except = array_map(function ($val) {
  118. return strtolower($val);
  119. }, $options['except']);
  120. if (in_array($this->request->action(), $except)) {
  121. return;
  122. }
  123. }
  124. call_user_func([$this, $method]);
  125. }
  126. /**
  127. * 加载模板输出
  128. * @access protected
  129. * @param string $template 模板文件名
  130. * @param array $vars 模板输出变量
  131. * @param array $config 模板参数
  132. * @return mixed
  133. */
  134. protected function fetch($template = '', $vars = [], $config = [])
  135. {
  136. return Response::create($template, 'view')->assign($vars)->config($config);
  137. }
  138. /**
  139. * 渲染内容输出
  140. * @access protected
  141. * @param string $content 模板内容
  142. * @param array $vars 模板输出变量
  143. * @param array $config 模板参数
  144. * @return mixed
  145. */
  146. protected function display($content = '', $vars = [], $config = [])
  147. {
  148. return Response::create($content, 'view')->assign($vars)->config($config)->isContent(true);
  149. }
  150. /**
  151. * 模板变量赋值
  152. * @access protected
  153. * @param mixed $name 要显示的模板变量
  154. * @param mixed $value 变量的值
  155. * @return $this
  156. */
  157. protected function assign($name, $value = '')
  158. {
  159. $this->view->assign($name, $value);
  160. return $this;
  161. }
  162. /**
  163. * 视图过滤
  164. * @access protected
  165. * @param Callable $filter 过滤方法或闭包
  166. * @return $this
  167. */
  168. protected function filter($filter)
  169. {
  170. $this->view->filter($filter);
  171. return $this;
  172. }
  173. /**
  174. * 初始化模板引擎
  175. * @access protected
  176. * @param array|string $engine 引擎参数
  177. * @return $this
  178. */
  179. protected function engine($engine)
  180. {
  181. $this->view->engine($engine);
  182. return $this;
  183. }
  184. /**
  185. * 设置验证失败后是否抛出异常
  186. * @access protected
  187. * @param bool $fail 是否抛出异常
  188. * @return $this
  189. */
  190. protected function validateFailException($fail = true)
  191. {
  192. $this->failException = $fail;
  193. return $this;
  194. }
  195. /**
  196. * 验证数据
  197. * @access protected
  198. * @param array $data 数据
  199. * @param string|array $validate 验证器名或者验证规则数组
  200. * @param array $message 提示信息
  201. * @param bool $batch 是否批量验证
  202. * @param mixed $callback 回调方法(闭包)
  203. * @return array|string|true
  204. * @throws ValidateException
  205. */
  206. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  207. {
  208. if (is_array($validate)) {
  209. $v = $this->app->validate();
  210. $v->rule($validate);
  211. } else {
  212. if (strpos($validate, '.')) {
  213. // 支持场景
  214. list($validate, $scene) = explode('.', $validate);
  215. }
  216. $v = $this->app->validate($validate);
  217. if (!empty($scene)) {
  218. $v->scene($scene);
  219. }
  220. }
  221. // 是否批量验证
  222. if ($batch || $this->batchValidate) {
  223. $v->batch(true);
  224. }
  225. if (is_array($message)) {
  226. $v->message($message);
  227. }
  228. if ($callback && is_callable($callback)) {
  229. call_user_func_array($callback, [$v, &$data]);
  230. }
  231. if (!$v->check($data)) {
  232. if ($this->failException) {
  233. throw new ValidateException($v->getError());
  234. }
  235. return $v->getError();
  236. }
  237. return true;
  238. }
  239. public function __debugInfo()
  240. {
  241. $data = get_object_vars($this);
  242. unset($data['app'], $data['request']);
  243. return $data;
  244. }
  245. }