用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
## 注解验证器 ThinkPHP支持使用注解方式定义路由和验证,需要安装额外的扩展: ``` composer require topthink/think-annotation ``` 然后可以直接在控制器类的方法注释中定义,例如: ~~~ <?php namespace app\controller; use think\annotation\Route; use think\annotation\route\Validate; use app\validate\IndexValidate; class Index { /** * @Validate(IndexValidate::class,scene="create",batch="true") * @return mixed * @Route("hello") */ public function hello() { return 'hello, TP6 Annotation Validate'; } } ~~~ `@Route("hello/:name")` 和 `@Validate(IndexValidate::class)` 就是注解路由和验证器的内容,请务必注意注释的规范,不能在注解路由里面使用单引号,否则可能导致注解路由解析失败,可以利用IDE生成规范的注释。如果你使用`PHPStorm`的话,建议安装`PHP Annotations`插件:[https://plugins.jetbrains.com/plugin/7320-php-annotations](https://plugins.jetbrains.com/plugin/7320-php-annotations),可以支持注解的自动完成。 然后需要声明上面引用验证器类,例如: ~~~ <?php namespace app\validate; use think\Validate; class IndexValidate extends Validate { protected $rule = [ 'name' => '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://img.kancloud.cn/eb/ca/ebcad4f348acf4c8f734d371ea8a6788_1002x142.png) ### 正确访问示例: ~~~ http://127.0.0.1:8000/hello?name=zhans ~~~ 页面输出 ![](https://img.kancloud.cn/54/9e/549eb02a19ac36e85f816ab32eb7d41d_2334x278.png)