12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?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->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(true);
- }
- return $v->failException(true)->check($data);
- }
- }
|