BaseController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app;
  4. use think\App;
  5. use think\exception\ValidateException;
  6. use think\Validate;
  7. /**
  8. * 控制器基础类
  9. */
  10. abstract class BaseController
  11. {
  12. /**
  13. * Request实例
  14. * @var \think\Request
  15. */
  16. protected $request;
  17. /**
  18. * 应用实例
  19. * @var \think\App
  20. */
  21. protected $app;
  22. protected $view;
  23. /**
  24. * 是否批量验证
  25. * @var bool
  26. */
  27. protected $batchValidate = false;
  28. /**
  29. * 控制器中间件
  30. * @var array
  31. */
  32. protected $middleware = [];
  33. /**
  34. * 构造方法
  35. * @access public
  36. * @param App $app 应用对象
  37. */
  38. public function __construct(App $app)
  39. {
  40. $this->app = $app;
  41. $this->request = $this->app->request;
  42. // 控制器初始化
  43. $this->initialize();
  44. }
  45. // 初始化
  46. protected function initialize()
  47. {}
  48. /**
  49. * 验证数据
  50. * @access protected
  51. * @param array $data 数据
  52. * @param string|array $validate 验证器名或者验证规则数组
  53. * @param array $message 提示信息
  54. * @param bool $batch 是否批量验证
  55. * @return array|string|true
  56. * @throws ValidateException
  57. */
  58. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  59. {
  60. if (is_array($validate)) {
  61. $v = new Validate();
  62. $v->rule($validate);
  63. } else {
  64. if (strpos($validate, '.')) {
  65. // 支持场景
  66. [$validate, $scene] = explode('.', $validate);
  67. }
  68. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  69. $v = new $class();
  70. if (!empty($scene)) {
  71. $v->scene($scene);
  72. }
  73. }
  74. $v->message($message);
  75. // 是否批量验证
  76. if ($batch || $this->batchValidate) {
  77. $v->batch(true);
  78. }
  79. return $v->failException(true)->check($data);
  80. }
  81. /**
  82. * 模板参数
  83. * @param type $name 参数名称
  84. * @param type $value 输出模板
  85. */
  86. protected function assign($name, $value) {
  87. $this->viewInit();
  88. $this->view->assign($name, $value);
  89. }
  90. /**
  91. * 输出格式
  92. */
  93. protected function display($template = '') {
  94. $this->viewInit();
  95. $request = request();
  96. if (empty($template))
  97. $template = $request->action();
  98. return $this->view->fetch($template);
  99. }
  100. private function viewInit(){
  101. if($this->view == null) {
  102. $app = config('app');
  103. $req = [
  104. '__IMAGES__' => $app['__IMAGES__'], // image变量
  105. '__JS__' => $app['__JS__'], // js变量
  106. '__CSS__' => $app['__CSS__'], // css变量
  107. '__ASSETS__' => $app['__ASSETS__'] //assets 变量
  108. ];
  109. $this->view = app('view');
  110. }
  111. }
  112. }