think\Loader::validate PHP Method

validate() public static method

实例化验证类 格式:[模块名/]验证器名
public static validate ( string $name = '', string $layer = 'validate', boolean $appendSuffix = false, string $common = 'common' ) : Object | false
$name string 资源地址
$layer string 验证层名称
$appendSuffix boolean 是否添加类名后缀
$common string 公共模块名
return Object | false
    public static function validate($name = '', $layer = 'validate', $appendSuffix = false, $common = 'common')
    {
        $name = $name ?: Config::get('default_validate');
        if (empty($name)) {
            return new Validate();
        }
        if (isset(self::$instance[$name . $layer])) {
            return self::$instance[$name . $layer];
        }
        if (strpos($name, '/')) {
            list($module, $name) = explode('/', $name);
        } else {
            $module = Request::instance()->module();
        }
        $class = self::parseClass($module, $layer, $name, $appendSuffix);
        if (class_exists($class)) {
            $validate = new $class();
        } else {
            $class = str_replace('\\' . $module . '\\', '\\' . $common . '\\', $class);
            if (class_exists($class)) {
                $validate = new $class();
            } else {
                throw new ClassNotFoundException('class not exists:' . $class, $class);
            }
        }
        self::$instance[$name . $layer] = $validate;
        return $validate;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * 登陆
  * @param  string $callback 登陆成功后的回调地址
  */
 public function index($callback = '')
 {
     if (IS_POST) {
         $validate = Loader::validate('Login');
         $data = $this->request->post();
         if (config('verify_code')) {
             $validateResult = $validate->check($data);
         } else {
             $validateResult = $validate->scene('not_verify')->check($data);
         }
         if (!$validateResult) {
             return $this->error($validate->getError(), '');
         }
         $user = Db::name('Member')->where('account', $data['account'])->find();
         if (!$user) {
             return $this->error('用户不存在', '');
         } elseif ($user['status'] != 1) {
             return $this->error('用户被禁用', '');
         } elseif ($user['password'] != umd5($data['password'])) {
             logs('登陆失败:密码错误', '', $user['id']);
             return $this->error('密码错误', '');
         } else {
             self::autoLogin($user);
             return $this->success('登陆成功', $callback ? $callback : url('system/index/index'));
         }
     } else {
         if (isLogin()) {
             $this->redirect(url('system/index/index'));
         }
         return view();
     }
 }
All Usage Examples Of think\Loader::validate