zircote/swagger-php
Generate OpenAPI 3.0/3.1/3.2 docs from your PHP 8.2+ code using native attributes (preferred) or optional Doctrine annotations. Includes CLI and programmatic generation, parses phpdoc, provides helpful error reporting, and powers interactive API docs.
API Documentation as a First-Class Citizen:
Developer Experience (DX) Improvements:
@OAT\Get, @OAT\Response), catching issues early via IDE tooling (e.g., PHPStorm’s attribute validation).Roadmap Priorities:
unevaluatedProperties).Build vs. Buy:
Generator hooks (e.g., add custom filters for internal endpoints).Use Cases:
/api/docs).securitySchemes in OpenAPI).unevaluatedProperties).swagger-codegen for other languages)."This package lets us automate 90% of API documentation by embedding specs directly in our PHP code. Instead of maintaining separate Swagger files that drift from reality, every endpoint’s contract is defined once—via PHP attributes—and stays in sync forever. This reduces onboarding time for new engineers, cuts documentation errors, and future-proofs our APIs for OpenAPI 3.2. For example, our
/usersendpoint’s Swagger UI will always reflect its actual parameters, responses, and auth requirements—no manual updates needed. The cost? A one-time migration to attributes (with backward-compatible PHPDoc support) and a CLI tool to generate docs on demand. ROI: fewer bugs, faster dev cycles, and happier API consumers."
*"swagger-php lets us write self-documenting APIs using PHP 8.2 attributes (e.g.,
@OAT\Post,@OAT\Response). Key wins:
- No more doc-schema drift: Docs are generated from code, so they’re always accurate.
- IDE-friendly: Attributes trigger autocompletion and validation (e.g., catch missing
descriptionin@OAT\Get).- Flexible output: Generate OpenAPI 3.0/3.1/3.2 specs as YAML/JSON, or use the CLI to auto-update
/api/docs.- Modern PHP support: Handles generics (
array<User|Admin>), unions, and complex types viasymfony/type-info.- CI/CD ready: Add a script to validate specs on PRs (e.g., fail if a new endpoint lacks a
@OAT\Infotitle).Migration path: Start with a single controller, then expand. Legacy PHPDoc annotations still work (but are deprecated). Example:
#[OAT\Get(path: '/users/{id}', responses: [new OAT\Response(ref: '#/components/schemas/User')])] public function show(User $user) { ... } ```"*
*"This ensures API contracts are version-controlled alongside code. Key benefits:
- Automated spec generation: Run
./vendor/bin/openapito update/api/docsin CI.- Early error detection: Invalid OpenAPI (e.g., missing
summaryin@OAT\Post) fails fast.- Tooling integration: Works with Swagger UI, Postman, and API gateways (e.g., Kong) out-of-the-box.
- Audit trail: All endpoints are documented by definition—no hidden or undocumented routes.
Action item: Add a pre-commit hook or GitHub Action to lint OpenAPI specs before merge."*
How can I help you explore Laravel packages today?