Controller.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. Loader::import('controller/Jump', TRAIT_PATH, EXT);
  15. class Controller
  16. {
  17. use Jump;
  18. /**
  19. * @var \think\View 视图类实例
  20. */
  21. protected $view;
  22. /**
  23. * @var \think\Request Request 实例
  24. */
  25. protected $request;
  26. /**
  27. * @var bool 验证失败是否抛出异常
  28. */
  29. protected $failException = false;
  30. /**
  31. * @var bool 是否批量验证
  32. */
  33. protected $batchValidate = false;
  34. /**
  35. * @var array 前置操作方法列表
  36. */
  37. protected $beforeActionList = [];
  38. /**
  39. * 构造方法
  40. * @access public
  41. * @param Request $request Request 对象
  42. */
  43. public function __construct(Request $request = null)
  44. {
  45. $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  46. $this->request = is_null($request) ? Request::instance() : $request;
  47. // 控制器初始化
  48. $this->_initialize();
  49. // 前置操作方法
  50. if ($this->beforeActionList) {
  51. foreach ($this->beforeActionList as $method => $options) {
  52. is_numeric($method) ?
  53. $this->beforeAction($options) :
  54. $this->beforeAction($method, $options);
  55. }
  56. }
  57. }
  58. /**
  59. * 初始化操作
  60. * @access protected
  61. */
  62. protected function _initialize()
  63. {
  64. }
  65. /**
  66. * 前置操作
  67. * @access protected
  68. * @param string $method 前置操作方法名
  69. * @param array $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
  70. * @return void
  71. */
  72. protected function beforeAction($method, $options = [])
  73. {
  74. if (isset($options['only'])) {
  75. if (is_string($options['only'])) {
  76. $options['only'] = explode(',', $options['only']);
  77. }
  78. if (!in_array($this->request->action(), $options['only'])) {
  79. return;
  80. }
  81. } elseif (isset($options['except'])) {
  82. if (is_string($options['except'])) {
  83. $options['except'] = explode(',', $options['except']);
  84. }
  85. if (in_array($this->request->action(), $options['except'])) {
  86. return;
  87. }
  88. }
  89. call_user_func([$this, $method]);
  90. }
  91. /**
  92. * 加载模板输出
  93. * @access protected
  94. * @param string $template 模板文件名
  95. * @param array $vars 模板输出变量
  96. * @param array $replace 模板替换
  97. * @param array $config 模板参数
  98. * @return mixed
  99. */
  100. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  101. {
  102. if ('' === $template) {
  103. $trace = debug_backtrace(false, 2);
  104. $suffix = Config::get('action_suffix');
  105. $action = $suffix ? substr($trace[1]['function'], 0, -strlen($suffix)) : $trace[1]['function'];
  106. $template = Loader::parseName($action);
  107. }
  108. return $this->view->fetch($template, $vars, $replace, $config);
  109. }
  110. /**
  111. * 渲染内容输出
  112. * @access protected
  113. * @param string $content 模板内容
  114. * @param array $vars 模板输出变量
  115. * @param array $replace 替换内容
  116. * @param array $config 模板参数
  117. * @return mixed
  118. */
  119. protected function display($content = '', $vars = [], $replace = [], $config = [])
  120. {
  121. return $this->view->display($content, $vars, $replace, $config);
  122. }
  123. /**
  124. * 模板变量赋值
  125. * @access protected
  126. * @param mixed $name 要显示的模板变量
  127. * @param mixed $value 变量的值
  128. * @return $this
  129. */
  130. protected function assign($name, $value = '')
  131. {
  132. $this->view->assign($name, $value);
  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 = Loader::validate();
  172. $v->rule($validate);
  173. } else {
  174. // 支持场景
  175. if (strpos($validate, '.')) {
  176. list($validate, $scene) = explode('.', $validate);
  177. }
  178. $v = Loader::validate($validate);
  179. !empty($scene) && $v->scene($scene);
  180. }
  181. // 批量验证
  182. if ($batch || $this->batchValidate) {
  183. $v->batch(true);
  184. }
  185. // 设置错误信息
  186. if (is_array($message)) {
  187. $v->message($message);
  188. }
  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. }