BaseController.php 2.9 KB

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