注解验证

最后更新于:2022-04-02 07:53:49

## 注解验证器 ThinkPHP支持使用注解方式定义路由和验证,需要安装额外的扩展: ``` composer require topthink/think-annotation ``` 然后可以直接在控制器类的方法注释中定义,例如: ~~~ 'require' ]; protected $message = [ 'name.require' => '姓名必须填写', ]; protected $scene = [ 'create' => ['name'], ]; } ~~~ >[danger] 该方式定义的路由在调试模式下面实时生效,部署模式则在第一次访问的时候生成注解缓存。 ### 注解验证器参数说明 > `value`参数,可以不写`value=`,直接抒写值就可以了 | 参数名 | 参数类型 | 参数默认值 | 参数说明 | | --- | --- | --- | --- | | value | string | | 验证器 | | scene | string | | 验证场景 | | batch | bool | true | 统一验证:true=是,flase=不是 | | message | array | [] | 错误内容 | ### 错误访问示例: ~~~ http://127.0.0.1:8000/hello ~~~ 页面输出 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/eb/ca/ebcad4f348acf4c8f734d371ea8a6788_1002x142.png) ### 正确访问示例: ~~~ http://127.0.0.1:8000/hello?name=zhans ~~~ 页面输出 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/54/9e/549eb02a19ac36e85f816ab32eb7d41d_2334x278.png)
';