Installation:
composer require ehyiah/apidoc-bundle
Ensure composer config extra.symfony.allow-contrib true is set.
First Use Case:
Create a basic YAML file at config/api_doc/swagger.yaml:
openapi: 3.0.0
info:
title: My API
version: 1.0.0
paths:
/api/users:
get:
summary: Get all users
responses:
200:
description: OK
Enable UI:
Configure config/packages/ehyiah_api_doc.yaml:
ehyiah_api_doc:
ui: swagger
Access Docs:
Visit /api/doc in your browser to see the Swagger UI.
YAML-First Workflow:
config/api_doc/ (e.g., users.yaml, auth.yaml).php bin/console apidoc:generate to validate and merge files into a single OpenAPI spec.!include in YAML:
paths:
!include users.yaml
PHP-Class Integration:
@ApiDoc attributes:
use Ehyiah\ApiDocBundle\Annotation\ApiDoc;
#[ApiDoc(
path: "/api/users",
methods: ["GET"],
summary: "Get all users"
)]
class UserController extends AbstractController { ... }
php bin/console apidoc:generate:classes
Hybrid Approach:
config/packages/ehyiah_api_doc.yaml:
ehyiah_api_doc:
sources:
- { type: yaml, path: "%kernel.project_dir%/config/api_doc" }
- { type: php, path: "%kernel.project_dir%/src/ApiDoc" }
UI Routing:
/docs) by overriding the controller:
ehyiah_api_doc:
ui: swagger
route:
path: /docs
controller: App\Controller\CustomApiDocController
Development:
apidoc:generate to validate.apidoc:serve to spin up a local Swagger UI server for testing:
php bin/console apidoc:serve
CI/CD:
apidoc:generate to your test suite to catch spec errors early.apidoc:generate:report for documentation sites.IDE Integration:
@ApiDoc attributes to link controllers to their docs in PhpStorm/VSCode via PHP Annotations.Caching Issues:
php bin/console cache:clear
config/packages/ehyiah_api_doc.yaml:
ehyiah_api_doc:
cache: false
YAML Parsing Errors:
apidoc:validate before merging:
php bin/console apidoc:validate config/api_doc/swagger.yaml
200: must be "200": in strict YAML).PHP Class Conflicts:
Ehyiah\ApiDocBundle\Model\AbstractApiDoc or implement Ehyiah\ApiDocBundle\Model\ApiDocInterface.User.yaml vs. User.php).UI Rendering:
ehyiah_api_doc:
ui: swagger # Default; switch to 'redoc' later
Log Generation:
Enable debug logs in config/packages/monolog.yaml:
handlers:
api_doc:
type: stream
path: "%kernel.logs_dir%/apidoc.log"
level: debug
Then check logs for parsing errors during apidoc:generate.
Dry Runs:
Use --dry-run to preview changes without writing files:
php bin/console apidoc:generate --dry-run
Schema Reuse:
schemas/user.yaml) and reference them:
components:
schemas:
User: !include schemas/user.yaml
Security Definitions:
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
paths:
paths:
/api/secure:
security:
- bearerAuth: []
Dynamic Paths:
#[ApiDoc(
path: "/api/users/{id}",
methods: ["GET"],
summary: "Get a user by ID",
parameters: [
new Parameter(name: "id", in: "path", required: true, schema: new \OpenApi\Schemas\Schema(type: "integer"))
]
)]
Extensions:
// src/ApiDoc/Generator/CustomGenerator.php
namespace App\ApiDoc\Generator;
use Ehyiah\ApiDocBundle\Generator\AbstractGenerator;
class CustomGenerator extends AbstractGenerator {
public function generate(): void {
parent::generate();
// Add custom logic here
}
}
Then configure in config/packages/ehyiah_api_doc.yaml:
ehyiah_api_doc:
generator: App\ApiDoc\Generator\CustomGenerator
Versioning:
paths:
/api/v1/users:
tags: [v1]
How can I help you explore Laravel packages today?