PHP8注解插件

v1.0.4 版本
2026-08-13 版本更新时间
39 安装
4 star

KingBes/Attribute

🚀 基于 webman 的 PHP 8 原生 Attribute(注解)路由方案,通过注解声明路由、中间件、权限标识、视图标识等信息。

环境要求

  • PHP >= 8.0
  • webman 框架

安装

composer require kingbes/attribute

快速开始

在控制器类或方法上添加 #[Annotation(...)] 注解,数组键值使用 => 语法。

use Kingbes\Attribute\Annotation;

#[Annotation([
    "title" => "首页控制器",
])]
class IndexController
{
    #[Annotation([
        "title"   => "首页",
        "path"    => ["/index", "/", "/home"],
        "request" => ["get", "post"],
        "auth"    => true,
    ])]
    public function index(Request $request)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }
}

注解参数

#[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([
    "name" => "other.hello",
])]
public function sayHello()
{
    // 模板中可用 {:route("other.hello")} 生成该方法的 URL
}

可重复注解

同一目标(类或方法)可声明多个 #[Annotation],重复注解会合并:

  • 标量键(如 title)后者覆盖前者;
  • path 数组会追加合并。
#[Annotation(["path" => ["/a"]])]
#[Annotation(["path" => ["/b"]])]
public function index()
{
    // 同时注册 /a 与 /b 两个路由
}

注解信息

通过 Data 类获取全部注解信息:

use Kingbes\Attribute\Data;

Data::$data  // 按 app / plugin 层级分组的全部注解

路由规则

  • 应用路由:/{控制器名下划线}/{方法名下划线}
  • 插件路由:/plugin/{作者}/{应用}/{控制器名下划线}/{方法名下划线}
  • 使用本插件后默认路由会被禁用,所有路由均需通过注解声明。

index 路由别名

index 方法除完整路径外,会自动注册一个去掉末尾 /index 的别名路径:

控制器 完整路径 别名路径
IndexController::index /index/index /
AboutController::index /about/index /about
user 应用 AboutController::index /user/about/index /user/about

限制

  • 控制器文件需位于 app/**/controllerplugin/**/app/**/controller 目录下,否则不会被扫描。
  • 注解数组键值必须使用 PHP 数组语法 =>,不能使用 :
  • 仅扫描公共方法,__construct__destruct 会被忽略。
赞助商