Controller.php 5.9 KB

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