Controller.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. protected $failException = false;
  28. // 是否批量验证
  29. protected $batchValidate = false;
  30. /**
  31. * 前置操作方法列表
  32. * @var array $beforeActionList
  33. * @access protected
  34. */
  35. protected $beforeActionList = [];
  36. /**
  37. * 构造方法
  38. * @param Request $request Request对象
  39. * @access public
  40. */
  41. public function __construct(Request $request = null)
  42. {
  43. if (is_null($request)) {
  44. $request = Request::instance();
  45. }
  46. $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  47. $this->request = $request;
  48. // 控制器初始化
  49. $this->_initialize();
  50. // 前置操作方法
  51. if ($this->beforeActionList) {
  52. foreach ($this->beforeActionList as $method => $options) {
  53. is_numeric($method) ?
  54. $this->beforeAction($options) :
  55. $this->beforeAction($method, $options);
  56. }
  57. }
  58. }
  59. // 初始化
  60. protected function _initialize()
  61. {
  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 $replace 模板替换
  94. * @param array $config 模板参数
  95. * @return mixed
  96. */
  97. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  98. {
  99. return $this->view->fetch($template, $vars, $replace, $config);
  100. }
  101. /**
  102. * 渲染内容输出
  103. * @access protected
  104. * @param string $content 模板内容
  105. * @param array $vars 模板输出变量
  106. * @param array $replace 替换内容
  107. * @param array $config 模板参数
  108. * @return mixed
  109. */
  110. protected function display($content = '', $vars = [], $replace = [], $config = [])
  111. {
  112. return $this->view->display($content, $vars, $replace, $config);
  113. }
  114. /**
  115. * 模板变量赋值
  116. * @access protected
  117. * @param mixed $name 要显示的模板变量
  118. * @param mixed $value 变量的值
  119. * @return void
  120. */
  121. protected function assign($name, $value = '')
  122. {
  123. $this->view->assign($name, $value);
  124. }
  125. /**
  126. * 初始化模板引擎
  127. * @access protected
  128. * @param array|string $engine 引擎参数
  129. * @return void
  130. */
  131. protected function engine($engine)
  132. {
  133. $this->view->engine($engine);
  134. }
  135. /**
  136. * 设置验证失败后是否抛出异常
  137. * @access protected
  138. * @param bool $fail 是否抛出异常
  139. * @return $this
  140. */
  141. protected function validateFailException($fail = true)
  142. {
  143. $this->failException = $fail;
  144. return $this;
  145. }
  146. /**
  147. * 验证数据
  148. * @access protected
  149. * @param array $data 数据
  150. * @param string|array $validate 验证器名或者验证规则数组
  151. * @param array $message 提示信息
  152. * @param bool $batch 是否批量验证
  153. * @param mixed $callback 回调方法(闭包)
  154. * @return array|string|true
  155. * @throws ValidateException
  156. */
  157. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  158. {
  159. if (is_array($validate)) {
  160. $v = Loader::validate();
  161. $v->rule($validate);
  162. } else {
  163. if (strpos($validate, '.')) {
  164. // 支持场景
  165. list($validate, $scene) = explode('.', $validate);
  166. }
  167. $v = Loader::validate($validate);
  168. if (!empty($scene)) {
  169. $v->scene($scene);
  170. }
  171. }
  172. // 是否批量验证
  173. if ($batch || $this->batchValidate) {
  174. $v->batch(true);
  175. }
  176. if (is_array($message)) {
  177. $v->message($message);
  178. }
  179. if ($callback && is_callable($callback)) {
  180. call_user_func_array($callback, [$v, &$data]);
  181. }
  182. if (!$v->check($data)) {
  183. if ($this->failException) {
  184. throw new ValidateException($v->getError());
  185. } else {
  186. return $v->getError();
  187. }
  188. } else {
  189. return true;
  190. }
  191. }
  192. }