rafoabbas/api-docs 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rafoabbas/api-docs

Composer 安装命令:

composer require rafoabbas/api-docs

包简介

Generate API documentation (Postman collections & OpenAPI specs) from PHP attributes and YAML files

README 文档

README

Generate API documentation (Postman collections & OpenAPI specs) from PHP 8 attributes and YAML files.

Requirements

  • PHP 8.2+
  • Laravel 11+

Installation

composer require rafoabbas/api-docs

Quick Start

Option 1: YAML Definitions (Recommended)

Define API endpoints in YAML files under resources/api-docs/:

# resources/api-docs/auth.yaml
folder: V1 / Auth

auth:
  type: bearer

requests:
  - name: Login
    method: POST
    uri: /v1/auth/login
    description: Authenticate user
    auth:
      type: noauth
    body:
      phone: "905551234567"
      password: "secret123"
    variables:
      - name: BEARER_TOKEN
        path: data.token

  - name: Get Profile
    method: GET
    uri: /v1/auth/me
    resource: App\Http\Resources\UserResource

Option 2: PHP Attributes

Add attributes to your controller:

use ApiDocs\Attributes\ApiFolder;
use ApiDocs\Attributes\ApiRequest;

#[ApiFolder('V1 / Auth')]
class AuthController extends Controller
{
    #[ApiRequest(name: 'Login', description: 'Authenticate user')]
    public function login(LoginRequest $request): JsonResponse
    {
        // ...
    }
}

Generate Documentation

php artisan api:generate

Output (when variable_scope is collection):

docs/
├── postman/
│   └── {timestamp}-collection.json
└── openapi/
    └── {timestamp}-openapi.yaml

Output (when variable_scope is environment):

docs/
├── postman/
│   ├── {timestamp}-collection.json
│   └── {timestamp}-local.postman_environment.json
└── openapi/
    └── {timestamp}-openapi.yaml

Command Options

php artisan api:generate --format=postman      # Only Postman
php artisan api:generate --format=openapi      # Only OpenAPI
php artisan api:generate --format=both         # Both (default)
php artisan api:generate --name="My API"       # Custom name
php artisan api:generate --output=api-docs     # Custom output dir
php artisan api:generate --openapi-format=json # OpenAPI as JSON
php artisan api:generate --exclude=admin       # Exclude prefixes

Attributes

Attribute Description
ApiRequest Request name, description, order
ApiFolder Group requests into folders
ApiBody Request body (supports FormRequest merge)
ApiResource Response Resource class
ApiVariable Extract response values to variables
ApiHeader Custom headers
ApiQueryParam Query parameters
ApiAuth Authentication config
ApiResponse Example responses
ApiTest Postman test scripts
ApiPreRequest Pre-request scripts
ApiHidden Exclude from docs
ApiDeprecated Mark endpoints as deprecated

Auto-Resolve Features

  • Request body from FormRequest rules()
  • Query parameters from FormRequest rules() (for GET/DELETE requests)
  • Response structure from Resource toArray()
  • Authentication from middleware (auth:sanctum, auth)
  • Route parameters from URI ({id}:id)
  • OpenAPI schema from validation rules (required|emailtype: string, format: email)
  • PHP Enum to OpenAPI enum conversion (via Rule::in() or Enum rule)
  • File upload auto-detection (file, image, mimes:multipart/form-data)
  • Pagination auto-detection (paginate() → paginated response schema)

Merging Strategy

YAML and PHP attributes can be used together. When both define the same endpoint (matched by method + uri):

  • YAML definitions take priority over PHP attributes
  • Merge is done field-by-field (non-null YAML fields override attribute fields)
  • Unmatched requests from both sources are included
  • YAML body_merge and body_except allow merging YAML body with auto-resolved FormRequest body

This allows a YAML-first workflow where controllers stay clean and all API documentation lives in YAML files.

Swagger UI

Interactive API documentation is available at /api/docs by default.

// config/api-docs.php
'swagger' => [
    'enabled' => true,
    'path' => '/api/docs',
    'middleware' => [],
    'dark_mode' => true,
    'persist_authorization' => true,
    'token' => env('API_DOCS_SWAGGER_TOKEN'),
],

Endpoints:

  • /api/docs - Swagger UI interface
  • /api/docs/openapi.json - OpenAPI specification

Protect with token:

API_DOCS_SWAGGER_TOKEN=your-secret-token

Access: /api/docs?token=your-secret-token

Disable in production:

API_DOCS_SWAGGER_ENABLED=false

Documentation

Full documentation is available on the Wiki:

Roadmap

Completed

  • Swagger UI integration - Interactive docs at /api/docs
  • Query parameter auto-resolve from FormRequest for GET/DELETE requests
  • Class-level ApiPreRequest support
  • YAML-first workflow with priority merge
  • YAML resource support for auto-resolving response from Resource classes
  • YAML body_merge / body_except for merging with FormRequest body
  • YAML hidden support to exclude requests from docs
  • YAML file-level shared auth, headers, pre_request_scripts
  • Configurable variable_scope (collection or environment)
  • Validation rules to OpenAPI schema (required|emailtype: string, format: email)
  • PHP Enum to OpenAPI enum conversion (via Rule::in() or Enum validation rule)
  • #[ApiDeprecated] - Mark endpoints as deprecated (with reason and replacement)
  • Pagination auto-detect (paginate() → paginated response schema with links/meta)
  • File upload - Auto multipart/form-data support (file, image, mimes: rules)

Export Formats

  • Markdown export
  • Insomnia export format
  • Bruno export format
  • Postman Cloud sync

Schema & Validation

  • JSON:API specification support

Attributes

  • #[ApiRateLimit] - Document rate limits
  • #[ApiWebhook] - Webhook documentation

Auto-Detection

  • Factory examples - Generate example data from Laravel factories

Authentication

  • OAuth2 flows documentation
  • Multiple auth schemes per endpoint

UI & Visualization

  • ReDoc UI - Alternative documentation UI
  • Scalar UI - Modern API documentation (scalar.com)
  • Code snippets - curl, JavaScript, Python, PHP examples
  • API changelog - Version diff documentation

Versioning

  • API versioning - Separate specs for v1, v2, etc.

License

MIT

rafoabbas/api-docs 适用场景与选型建议

rafoabbas/api-docs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 163 次下载、GitHub Stars 达 23, 最近一次更新时间为 2026 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「documentation」 「rest」 「api」 「attributes」 「laravel」 「swagger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rafoabbas/api-docs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rafoabbas/api-docs 我们能提供哪些服务?
定制开发 / 二次开发

基于 rafoabbas/api-docs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 163
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-29