oro/api-doc-bundle
Fork of NelmioApiDocBundle 2.x updated for Symfony 5 compatibility. Generates API documentation with a Swagger-UI-inspired interface, including routes, parameters, and responses, with PHPUnit tests and MIT license.
Laravel Compatibility:
The oro/api-doc-bundle (NelmioApiDocBundle fork) is Symfony-centric, requiring a hybrid integration approach for Laravel. Key considerations:
symfony/serializer, symfony/http-kernel, and symfony/dependency-injection as drop-in replacements where possible.spatie/laravel-annotation-reader to parse @ApiDoc annotations (Laravel lacks native support).Route::getRoutes()) instead of Symfony’s router.Events facade or custom middleware.Recommended Stack:
| Laravel Feature | Integration Strategy | Tools/Libraries |
|---|---|---|
| Routing | Parse Laravel routes → adapt to OpenAPI spec | spatie/laravel-openapi (partial) |
| Annotations | Custom parser or spatie/laravel-annotation-reader |
doctrine/annotations (fallback) |
| Serialization | symfony/serializer or spatie/array-to-xml |
jms/serializer (legacy) |
| Dependency Injection | Laravel’s container or PHP-DI | php-di/php-di |
| HTTP Layer | Symfony’s HttpFoundation via symfony/http-foundation |
symfony/http-kernel (for kernel events) |
Avoid:
NelmioApiDocBundle usage without abstraction (Symfony lock-in).Phase 1: Assessment (1–2 weeks)
oro/api-doc-bundle against Laravel-native alternatives:
darkajp/l5-swagger (Laravel 5/6/7/8).zircote/swagger-php (pure PHP, annotation-based).spatie/laravel-openapi (modern, annotation-driven).oro/api-doc-bundle offers unique features (e.g., advanced Symfony annotation support), proceed; otherwise, favor Laravel-native tools.Phase 2: Proof of Concept (2–3 weeks)
NelmioApiDocBundle/Generator/OpenApiGenerator) into a standalone PHP library.RouteCollection format.ContainerInterface, EventDispatcher) using Laravel’s equivalents.// app/Services/OpenApiAdapter.php
use Symfony\Component\Routing\RouteCollection;
use Illuminate\Support\Facades\Route;
class OpenApiAdapter
{
public function getRouteCollection(): RouteCollection
{
$collection = new RouteCollection();
foreach (Route::getRoutes() as $route) {
$symfonyRoute = new \Symfony\Component\Routing\Route(
$route->uri(),
$route->methods(),
$route->getAction()['uses']
);
$collection->add($route->getName(), $symfonyRoute);
}
return $collection;
}
}
Phase 3: Full Integration (3–4 weeks)
laravel-nelmio-api-doc) to wrap the bundle’s logic:
publishes (e.g., config/nelmio_api_doc.php).php artisan api:docs).spatie/laravel-annotation-reader to parse @ApiDoc:
$reader = new \Spatie\LaravelAnnotationReader\AnnotationReader();
$annotations = $reader->getMethodAnnotations(new \ReflectionMethod(UserController::class, 'show'));
Route::get('/api/doc.json', [OpenApiController::class, 'getJson']);
darkajp/l5-swagger for UI or use a standalone Swagger UI instance.Phase 4: CI/CD & Validation (1–2 weeks)
swagger-cli:
# .github/workflows/api-docs.yml
- name: Validate API Docs
run: |
composer require swagger-api/swagger-cli
vendor/bin/swagger-cli validate ./storage/api-doc.json
storage/api-doc.json and cache with laravel-cache:
Cache::remember('api-docs', now()->addHours(1), function () {
return $this->generateOpenApi();
});
symfony/http-client v4.nelmio/api-doc-bundle and oro/api-doc-bundle (use only the fork).symfony/* packages (e.g., symfony/serializer) over jms/serializer for consistency./api/v1/users) before scaling.@ApiDoc(resource=true) for resources, then add @ApiDoc(description=...) for methods.oro/api-doc-bundle for updates (low activity; prefer upstream nelmio/api-doc-bundle v3.x if available).composer.json and backport critical fixes manually.OpenApiAdapter and annotation parser will require updates if Laravel’s routing or annotation systems change.symfony/serializer) for breaking changes.docs/API_DOCS.md with:
@ApiDoc annotations.php artisan api:docs).## Common Annotations
- `@ApiDoc(resource=true)`: Marks a resource (e.g., `/users`).
- `@ApiDoc(description="...")`: Adds method-level docs.
- `@ApiDoc(
parameters={
@ApiDocParameter(name="id", description="User ID", required=true)
}
)`: Documents parameters.
nelmio/api-doc-bundle GitHub for core bugs.laravel-nelmio-api-doc repo.Cache::forever('api-docs', $this->generateOpenApi());
How can I help you explore Laravel packages today?