Download the PHP package sorry510/annotation without Composer
On this page you can find all versions of the php package sorry510/annotation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sorry510/annotation
More information about sorry510/annotation
Files in sorry510/annotation
Package annotation
Short Description laravel annotation
License MIT
Informations about the package annotation
安装
使用说明
功能
添加了 4 中注解
@Validator参数验证(只能使用在Controller中)@RequestParam添加默认参数,传入同名参数会覆盖默认参数(只能使用在Controller中)@Transaction添加事务(只能使用在Controller中)@Inject注入属性(注意:循环注入可能会有循环依赖的问题a->b->c->a)
使用前提
-
生成配置文件
-
在对应的
Controller文件中添加对应的注解的命名空间 - 在
app\Http\Kernel.php添加annotation中间件
@Validator
Validator(class, scene)有 2 个参数 class 和 scene,
- class 是选用的验证器的类的完整路径
(必填) - scene 是场景
(可选)
当验证没有通过时,会抛出
异常被捕获,直接返回
Validate 类的写法
示例
@RequestParam
RequestParam(fields, method)有 2 个参数 fields 和 method,
- fields 定义要获取的字段名,可批量设置默认值,如果前端传入了对应 key 可以覆盖默认值
(必填) - method 获取参数的方法,支持 get、post、put、delete,不填写默认是 param
(可选)
示例
ps: 注意默认是数组的写法是用
{}而不是[], 字符串必须用", 不能用'
@Transaction
示例
ps: 事务对http的返回code码为200~300之间的自动提交,其它的进行回滚操作
@Inject
Inject(args)有 1 个参数 args,
- args 定义类的初始化参数
(可选,可以在注入自定义属性内容)
在注释中添加
@var xxxclass注释想要被注入的类即可实现注入,注入为循环模式,最多2层深度,相当于可以在 controller 层注入任意的类, 同时在这个任意的类注入另一些类,当被注入的类有构造函数__contruct时,会依据构造函数实例化,构造函数中含有的实体类参数也会被自动注入,非实体类参数可以通过args传参传入(非实体类参数必须放到实体类参数后面)
完整示例
controller 层注入
循环注入属性,有如下 2 中方式
-
非构造函数方式继续使用
Inject注入 -
构造函数方式注入
- 构造函数方式传参注入(非实体类参数必须放到实体类参数后面)
All versions of annotation with dependencies
ext-json Version *
doctrine/annotations Version ^1.12
php-di/phpdoc-reader Version ^2.2