123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- declare (strict_types=1);
- namespace crmeb\basic;
- use think\facade\App;
- use think\exception\HttpResponseException;
- use think\exception\ValidateException;
- use think\facade\View;
- use think\facade\Validate;
- abstract class BaseController
- {
-
- protected $request;
-
- protected $app;
-
- protected $batchValidate = false;
-
- protected $middleware = [];
-
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->request = 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, '.')) {
-
- list($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);
- }
-
- protected function assign(...$vars)
- {
- View::assign(...$vars);
- }
-
- protected function fetch(string $template = '')
- {
- return View::fetch($template);
- }
-
- protected function redirect(...$args){
- throw new HttpResponseException(redirect(...$args));
- }
- }
|