Controller.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. return $this->view->fetch($template, $vars, $replace, $config);
  103. }
  104. /**
  105. * 渲染内容输出
  106. * @access protected
  107. * @param string $content 模板内容
  108. * @param array $vars 模板输出变量
  109. * @param array $replace 替换内容
  110. * @param array $config 模板参数
  111. * @return mixed
  112. */
  113. protected function display($content = '', $vars = [], $replace = [], $config = [])
  114. {
  115. return $this->view->display($content, $vars, $replace, $config);
  116. }
  117. /**
  118. * 模板变量赋值
  119. * @access protected
  120. * @param mixed $name 要显示的模板变量
  121. * @param mixed $value 变量的值
  122. * @return $this
  123. */
  124. protected function assign($name, $value = '')
  125. {
  126. $this->view->assign($name, $value);
  127. return $this;
  128. }
  129. /**
  130. * 初始化模板引擎
  131. * @access protected
  132. * @param array|string $engine 引擎参数
  133. * @return $this
  134. */
  135. protected function engine($engine)
  136. {
  137. $this->view->engine($engine);
  138. return $this;
  139. }
  140. /**
  141. * 设置验证失败后是否抛出异常
  142. * @access protected
  143. * @param bool $fail 是否抛出异常
  144. * @return $this
  145. */
  146. protected function validateFailException($fail = true)
  147. {
  148. $this->failException = $fail;
  149. return $this;
  150. }
  151. /**
  152. * 验证数据
  153. * @access protected
  154. * @param array $data 数据
  155. * @param string|array $validate 验证器名或者验证规则数组
  156. * @param array $message 提示信息
  157. * @param bool $batch 是否批量验证
  158. * @param mixed $callback 回调方法(闭包)
  159. * @return array|string|true
  160. * @throws ValidateException
  161. */
  162. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  163. {
  164. if (is_array($validate)) {
  165. $v = Loader::validate();
  166. $v->rule($validate);
  167. } else {
  168. // 支持场景
  169. if (strpos($validate, '.')) {
  170. list($validate, $scene) = explode('.', $validate);
  171. }
  172. $v = Loader::validate($validate);
  173. !empty($scene) && $v->scene($scene);
  174. }
  175. // 批量验证
  176. if ($batch || $this->batchValidate) {
  177. $v->batch(true);
  178. }
  179. // 设置错误信息
  180. if (is_array($message)) {
  181. $v->message($message);
  182. }
  183. // 使用回调验证
  184. if ($callback && is_callable($callback)) {
  185. call_user_func_array($callback, [$v, &$data]);
  186. }
  187. if (!$v->check($data)) {
  188. if ($this->failException) {
  189. throw new ValidateException($v->getError());
  190. }
  191. return $v->getError();
  192. }
  193. return true;
  194. }
  195. }