Web开发过程中,经常需要进行参数校验。在laravel中我们经常使用validator或者request来进行验证,但是这两种验证对于自定义提示信息和自定义验证规则来说都不是很方便,下面介绍一个非常方便的用法:新建一个抽象类值数组**@vararray*/protected$data=array();/***验证错误**@vararray*/protected$errors=array();/***验证规则**@vararray*/protected$rules=array();/***验证信息**@vararray*/protected$messages=array();/***验证码**@vararray*/protected$codes=array();公共函数__construct(array$data){$this->data=$data;$this->before();$this->validator=Validator::make($this->data,$this->rules,$this->messages);$this->after();}/***设置要验证的数据**@returnvalidator*/publicfunctiongetValidator(){return$this->validator;}/***设置数据以验证**@return$this*/publicfunctionwith(array$data){$this->data=$data;$this->before();$this->validator=$this->validator->make($this->data,$this->rules,$this->messages);$this->after();返回$这个;}/***验证通过或失败**@returnboolean*/publicfunctionpasses(){if($this->validator->fails()){$this->errors=$this->validator->messages();返回假;}返回真;}/***返回错误,如果有的话**@returnarray*/publicfunctionerrors(){return$this->errors;}/***返回错误代码,如果有的话**@returnarray*/publicfunctiongetCodes(){return$this->codes;}/***getRules**@returnarray*/publicfunctiongetRules(){return$this->rules;}/***getData**@returnarray*/publicfunctiongetData(){return$this->data;}/***getErrors**@returnarray*/publicfunctiongetErrors(){return$this->errors;}/***getMessages**@returnarray*/publicfunctiongetMessages(){return$this->messages;}/***setRule**@paramstring$key*@paramstring$value**@return$this*/publicfunctionsetRule($key,$value){$this->rules[$key]=$value;返回$这个;}/***emptyRules**@return$this*/publicfunctionemptyRules(){$this->rules=array();返回$这个;}/***sometimes**@paramstring$attribute*@paramstring|array$rules*@paramcallable$callback**@return$this*/publicfunctionsometimes($attribute,$rules,callable$callback){$this->validator->sometimes($attribute,$rules,$callback);返回$这个;}/***resolver**@paramClosure$resolver**@return$this*/publicfunctionresolver(Closure$resolver){Validator::resolver($resolver);返回$这个;}/***replacer**@paramClosure$resolver**@return$this*/publicfunctionreplacer($replace,Closure$resolver){Validator::replacer($replace,$resolver);返回$这个;}/***extendImplicit**@paramClosure$resolver**@return$this*/publicfunctionextendImplicit($extendImplicit,Closure$resolver){Validator::extendImplicit($extendImplicit,$resolver);返回$这个;}/***extend**@paramstring$rule*@param\Closure|string$extension*@paramstring$message**@return$this*/publicfunctionextend($rule,$extension,$message=null){Validator::extend($rule,$extension,$message);返回$这个;}/***before(extend(),resolver())**@return$this*/publicfunctionbefore(){}/***after(sometimes())**@return$this*/publicfunctionafter(){}}新建中间件isMethod('POST')){$type=$request->segment(1);如果($validator){$validator=$this->namespace.'\\'。studly_case($type)。'\\'。studly_case($validator)。'验证器';$validator=new$validator($request->all());如果(!$validator->passes()){如果($request->isAjax()){返回$validator->errors()->first();}else{returnredirect()->back()->withErrors($validator->getV验证器())->withInput();}}}}返回$next($request);}}NewTestTestValidator['必需','测试','min:1'],);/***验证消息**@varArray*/protected$messages=array('name.required'=>'required','name.min'=>'最少1个字符','name.test'=>'测试',);/***自定义验证规则或扩展Validator类*/publicfunctionbefore(){$this->extend('test',function($attribute,$value,$parameters){returnbool;});如何使用Route::post('/',['middleware'=>['valiAdmin:Test'],'uses'=>'IndexController@test']);在路由中实现validation和controller的分离,verifymore方便快捷具体使用可以自己配置优化~
