123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- declare (strict_types=1);
- namespace app;
- use think\App;
- use think\exception\ValidateException;
- use think\Validate;
- abstract class BaseController
- {
-
- protected $request;
-
- protected $app;
-
- protected $batchValidate = false;
-
- protected $middleware = [];
-
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->request = $this->app->request;
- $this->request->controllerPath = str_replace('.', '/', $this->request->controller(true));
-
- $this->initialize();
- }
-
- protected function initialize()
- {
- }
-
- protected function validate(array $data, $validate, array $message = [], bool $batch = false)
- {
- if (is_array($validate)) {
- $v = new Validate();
- $v->rule($validate);
- } else {
- if (strpos($validate, '.')) {
-
- [$validate, $scene] = explode('.', $validate);
- }
- $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
- $v = new $class();
- if (!empty($scene)) {
- $v->scene($scene);
- }
- }
- $v->message($message);
-
- if ($batch || $this->batchValidate) {
- $v->batch();
- }
- return $v->failException()->check($data);
- }
- }
|