validate方法可以接收HTTP请求(request)和一系列规则验证(Array类型),如果验证通过,则继续运行下面代码。如果验证失败,则会重定向回上一个位置。
如果想要自定义错误格式,需要引入Illuminate\Contracts\Validation\Validator类,使用方法如下:
1 | use Illuminate\Support\Facades\Validator; |
验证失败返回结果如下:
The appid field is required.
如果return $validate->errors()->all(); 则返回:
[“The appid field is required.”,”The secret key field is required.”,”The source page field is required.”]
如果想自定义返回内容,可以这样写:
1 | if($validate->fails()) { |
这样就会跳转到你自定义好的error路由。