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. \think\Loader::import('controller/Jump', TRAIT_PATH, EXT);
  13. use think\exception\ValidateException;
  14. class Controller
  15. {
  16. use \traits\controller\Jump;
  17. /**
  18. * @var \think\View 视图类实例
  19. */
  20. protected $view;
  21. /**
  22. * @var \think\Request Request实例
  23. */
  24. protected $request;
  25. // 验证失败是否抛出异常
  26. protected $failException = false;
  27. // 是否批量验证
  28. protected $batchValidate = false;
  29. /**
  30. * 前置操作方法列表
  31. * @var array $beforeActionList
  32. * @access protected
  33. */
  34. protected $beforeActionList = [];
  35. /**
  36. * 构造方法
  37. * @param Request $request Request对象
  38. * @access public
  39. */
  40. public function __construct(Request $request = null)
  41. {
  42. if (is_null($request)) {
  43. $request = Request::instance();
  44. }
  45. $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  46. $this->request = $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. protected function _initialize()
  60. {
  61. }
  62. /**
  63. * 前置操作
  64. * @access protected
  65. * @param string $method 前置操作方法名
  66. * @param array $options 调用参数 ['only'=>[...]] 或者['except'=>[...]]
  67. */
  68. protected function beforeAction($method, $options = [])
  69. {
  70. if (isset($options['only'])) {
  71. if (is_string($options['only'])) {
  72. $options['only'] = explode(',', $options['only']);
  73. }
  74. if (!in_array($this->request->action(), $options['only'])) {
  75. return;
  76. }
  77. } elseif (isset($options['except'])) {
  78. if (is_string($options['except'])) {
  79. $options['except'] = explode(',', $options['except']);
  80. }
  81. if (in_array($this->request->action(), $options['except'])) {
  82. return;
  83. }
  84. }
  85. call_user_func([$this, $method]);
  86. }
  87. /**
  88. * 加载模板输出
  89. * @access protected
  90. * @param string $template 模板文件名
  91. * @param array $vars 模板输出变量
  92. * @param array $replace 模板替换
  93. * @param array $config 模板参数
  94. * @return mixed
  95. */
  96. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  97. {
  98. return $this->view->fetch($template, $vars, $replace, $config);
  99. }
  100. /**
  101. * 渲染内容输出
  102. * @access protected
  103. * @param string $content 模板内容
  104. * @param array $vars 模板输出变量
  105. * @param array $replace 替换内容
  106. * @param array $config 模板参数
  107. * @return mixed
  108. */
  109. protected function display($content = '', $vars = [], $replace = [], $config = [])
  110. {
  111. return $this->view->display($content, $vars, $replace, $config);
  112. }
  113. /**
  114. * 模板变量赋值
  115. * @access protected
  116. * @param mixed $name 要显示的模板变量
  117. * @param mixed $value 变量的值
  118. * @return void
  119. */
  120. protected function assign($name, $value = '')
  121. {
  122. $this->view->assign($name, $value);
  123. }
  124. /**
  125. * 初始化模板引擎
  126. * @access protected
  127. * @param array|string $engine 引擎参数
  128. * @return void
  129. */
  130. protected function engine($engine)
  131. {
  132. $this->view->engine($engine);
  133. }
  134. /**
  135. * 设置验证失败后是否抛出异常
  136. * @access protected
  137. * @param bool $fail 是否抛出异常
  138. * @return $this
  139. */
  140. protected function validateFailException($fail = true)
  141. {
  142. $this->failException = $fail;
  143. return $this;
  144. }
  145. /**
  146. * 验证数据
  147. * @access protected
  148. * @param array $data 数据
  149. * @param string|array $validate 验证器名或者验证规则数组
  150. * @param array $message 提示信息
  151. * @param bool $batch 是否批量验证
  152. * @param mixed $callback 回调方法(闭包)
  153. * @return array|string|true
  154. * @throws ValidateException
  155. */
  156. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  157. {
  158. if (is_array($validate)) {
  159. $v = Loader::validate();
  160. $v->rule($validate);
  161. } else {
  162. if (strpos($validate, '.')) {
  163. // 支持场景
  164. list($validate, $scene) = explode('.', $validate);
  165. }
  166. $v = Loader::validate($validate);
  167. if (!empty($scene)) {
  168. $v->scene($scene);
  169. }
  170. }
  171. // 是否批量验证
  172. if ($batch || $this->batchValidate) {
  173. $v->batch(true);
  174. }
  175. if (is_array($message)) {
  176. $v->message($message);
  177. }
  178. if ($callback && is_callable($callback)) {
  179. call_user_func_array($callback, [$v, &$data]);
  180. }
  181. if (!$v->check($data)) {
  182. if ($this->failException) {
  183. throw new ValidateException($v->getError());
  184. } else {
  185. return $v->getError();
  186. }
  187. } else {
  188. return true;
  189. }
  190. }
  191. }