Controller.php 6.2 KB

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