Pros:
symfony/http-foundation).@OA\Tag, @OA\Info), reducing manual maintenance.Cons:
Route::get()) and middleware stack differ from Symfony’s Controller/Kernel model. Requires abstraction layers (e.g., api-platform/core or custom bridges).Symfony Compatibility:
HttpFoundation, Serializer, and PropertyAccess components. Laravel can integrate these via:
symfony/http-foundation, symfony/serializer, symfony/property-access.HttpFoundationBridgeServiceProvider).ApiResource classes). Laravel’s router would need to delegate to API Platform’s ResourceClassResolver or use middleware to intercept requests.Laravel-Specific Challenges:
OpenApiContextListener) may conflict with Laravel’s Kernel. Requires custom middleware to bridge the two.Route annotations (e.g., #[Route('/users')]) differ from API Platform’s @ApiResource. May need a hybrid approach (e.g., using api-platform/core’s Resource classes alongside Laravel routes).Validator. Laravel’s Illuminate\Validation would need to be mapped or disabled for API Platform routes.routes/api.php or api.php may require custom route loading logic.ApiResource classes for new endpoints to avoid mixing paradigms.#[OA\...]) is easier in newer Laravel.#[OA\Info]). Laravel 9+ supports this.symfony/http-foundation, symfony/serializer, and symfony/property-access are Laravel-compatible via Composer.Validator can replace or supplement Laravel’s Validator for API Platform routes.Security component may conflict with Laravel’s Auth system (e.g., Sanctum, Passport). Requires custom guards.Route::model() bindings won’t integrate natively.OpenApiContextListener) must be inserted before Laravel’s StartSession/ShareErrorsFromSession to avoid doc generation failures.Phase 1: Documentation-Only Mode
api-platform/openapi and symfony dependencies.// app/Console/Commands/GenerateOpenApi.php
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
use Symfony\Component\Serializer\SerializerInterface;
public function handle() {
$openApiFactory = new OpenApiFactory();
$serializer = app(SerializerInterface::class);
$openApi = $openApiFactory->__invoke($serializer, [], []);
file_put_contents(public_path('openapi.json'), $openApi->toJson());
}
/public/openapi.json for Swagger UI.Phase 2: Hybrid Routing
@ApiResource and @OA\* tags.Route::group to delegate API Platform routes:
Route::prefix('api')->group(function () {
// Laravel routes...
});
Route::prefix('api-platform')->group(function () {
// API Platform routes (via middleware)
});
ResourceClassResolver in Laravel’s service container.Phase 3: Full Integration
RouteServiceProvider with a custom provider that merges Laravel and API Platform routing.Validator with Symfony’s for API Platform routes.symfony/bridge packages (e.g., symfony/http-foundation-bundle) or manually bind services.// app/Providers/OpenApiServiceProvider.php
public function register() {
$this->app->bind(\Symfony\Component\HttpFoundation\Request::class, function () {
return Request::capture();
});
}
#[OA\Tag]). For older versions, use annotations (requires doctrine/annotations).use OpenApi\Annotations as OA;
#[OA\Tag(name: "Users")]
class UserController extends Controller {
#[OA\Get(path: "/users", ...)]
public function index() { ... }
}
Validator can validate requests before Laravel’s middleware. Use a priority middleware:
$router->middleware('api-platform.validation')->group(function () {
// API Platform routes
});
| Step | Task | Dependencies | Risk |
|---|---|---|---|
| 1 | Install dependencies (api-platform/openapi, Symfony components) |
None | Low |
| 2 | Generate static OpenAPI schema (no route changes) | Step 1 | Low |
| 3 | Annotate new controllers with @ApiResource/@OA\* |
Step 2 | Medium |
| 4 | Register API Platform routes via Laravel middleware | Step 3 | High |
| 5 | Replace Laravel’s Validator for API Platform routes |
Step 4 | High |
| 6 | Merge routing logic (custom RouteServiceProvider) |
Step 5 | Critical |
| 7 | Deploy with Swagger UI integration | Step 6 | Medium |
@OA\* tags live near route logic, improving discoverability.How can I help you explore Laravel packages today?