BaseController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. declare (strict_types=1);
  12. namespace crmeb\basic;
  13. use think\facade\App;
  14. use think\exception\HttpResponseException;
  15. use think\exception\ValidateException;
  16. use think\facade\View;
  17. use think\facade\Validate;
  18. /**
  19. * 控制器基础类
  20. */
  21. abstract class BaseController
  22. {
  23. /**
  24. * Request实例
  25. * @var \think\Request
  26. */
  27. protected $request;
  28. /**
  29. * 应用实例
  30. * @var \think\App
  31. */
  32. protected $app;
  33. /**
  34. * 是否批量验证
  35. * @var bool
  36. */
  37. protected $batchValidate = false;
  38. /**
  39. * 控制器中间件
  40. * @var array
  41. */
  42. protected $middleware = [];
  43. /**
  44. * 构造方法
  45. * @access public
  46. * @param App $app 应用对象
  47. */
  48. public function __construct(App $app)
  49. {
  50. $this->app = $app;
  51. // $this->request = $this->app->request;
  52. $this->request = app('request');
  53. // 控制器初始化
  54. $this->initialize();
  55. }
  56. // 初始化
  57. protected function initialize()
  58. {
  59. }
  60. /**
  61. * 验证数据
  62. * @access protected
  63. * @param array $data 数据
  64. * @param string|array $validate 验证器名或者验证规则数组
  65. * @param array $message 提示信息
  66. * @param bool $batch 是否批量验证
  67. * @return array|string|true
  68. * @throws ValidateException
  69. */
  70. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  71. {
  72. if (is_array($validate)) {
  73. $v = new Validate();
  74. $v->rule($validate);
  75. } else {
  76. if (strpos($validate, '.')) {
  77. // 支持场景
  78. list($validate, $scene) = explode('.', $validate);
  79. }
  80. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  81. $v = new $class();
  82. if (!empty($scene)) {
  83. $v->scene($scene);
  84. }
  85. }
  86. $v->message($message);
  87. // 是否批量验证
  88. if ($batch || $this->batchValidate) {
  89. $v->batch(true);
  90. }
  91. return $v->failException(true)->check($data);
  92. }
  93. /**
  94. * 模板赋值
  95. * @param mixed ...$vars
  96. */
  97. protected function assign(...$vars)
  98. {
  99. View::assign(...$vars);
  100. }
  101. /**
  102. * 解析和获取模板内容
  103. * @param string $template
  104. * @return string
  105. * @throws \Exception
  106. */
  107. protected function fetch(string $template = '')
  108. {
  109. return View::fetch($template);
  110. }
  111. /**
  112. * 重定向
  113. * @param mixed ...$args
  114. */
  115. protected function redirect(...$args){
  116. throw new HttpResponseException(redirect(...$args));
  117. }
  118. }