1
0

Controller.php 6.4 KB

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