Download the PHP package kingbes/attribute without Composer
On this page you can find all versions of the php package kingbes/attribute. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Please rate this library. Is it a good library?
Informations about the package attribute
KingBes/Attribute
🚀 基于 webman 的 PHP 8 原生 Attribute(注解)路由方案,通过注解声明路由、中间件、权限标识、视图标识等信息。
环境要求
- PHP >= 8.0
- webman 框架
安装
快速开始
在控制器类或方法上添加 #[Annotation(...)] 注解,数组键值使用 => 语法。
注解参数
#[Annotation(array $data)] 支持以下键:
| 键 | 类型 | 必须 | 说明 |
|---|---|---|---|
title |
string | 否 | 标题标识,用于权限/视图等场景 |
path |
string[] | 否 | 自定义路由路径。为空时自动使用类名 + 方法名生成路由 |
request |
string[] | 否 | 允许的 HTTP 方法,见下方默认值 |
middleware |
string[] | 否 | 路由中间件,默认 [] |
auth |
bool | 否 | 权限标识 |
name |
string | 否 | 自定义路由名(webman ->name()),可覆盖默认名 |
默认值
request默认:['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']middleware默认:[]path默认:[],为空时自动取类名下划线/方法名下划线作为路由name默认:应用.控制器.方法
自定义路由名
name 用于设置 webman 路由名(->name()),可在模板中通过 {:route("路由名")} 反查生成 URL,无需手写路径。指定后覆盖该方法的默认路由名:
可重复注解
同一目标(类或方法)可声明多个 #[Annotation],重复注解会合并:
- 标量键(如
title)后者覆盖前者; path数组会追加合并。
注解信息
通过 Data 类获取全部注解信息:
路由规则
- 应用路由:
/{控制器名下划线}/{方法名下划线} - 插件路由:
/plugin/{作者}/{应用}/{控制器名下划线}/{方法名下划线} - 使用本插件后默认路由会被禁用,所有路由均需通过注解声明。
index 路由别名
index 方法除完整路径外,会自动注册一个去掉末尾连续 index 段的别名路径:
| 控制器 | 完整路径 | 别名路径 |
|---|---|---|
IndexController::index(单应用根) |
/index/index |
/ |
默认 index 应用 IndexController::index |
/index/index/index |
/ |
admin 应用 IndexController::index |
/admin/index/index |
/admin |
AboutController::index |
/about/index |
/about |
user 应用 AboutController::index |
/user/about/index |
/user/about |
规则:从完整路径末尾依次去掉 /index 段,直到不剩或剩余非 index 段。如 /admin/index/index → /admin,/index/index/index → /。如需自定义别名行为,可在 index 方法上用 path 显式指定。
测试
在 webman 项目中用 route:list 命令查看注解生成的路由:
启动服务后逐个访问验证:
检查点:
- 默认路由(
/{控制器名下划线}/{方法名下划线}) index别名路由(去掉末尾/index)- 自定义
path与自定义路由名name
限制
- 控制器文件需位于
app/**/controller或plugin/**/app/**/controller目录下,否则不会被扫描。 - 注解数组键值必须使用 PHP 数组语法
=>,不能使用:。 - 仅扫描公共方法,
__construct、__destruct会被忽略。
All versions of attribute with dependencies
PHP Build Version
Package Version
Requires
php Version
>8.0
The package kingbes/attribute contains the following files
Loading the files please wait ...