- Can I use API Platform Core (dunglas/api-bundle) in Laravel, or is it only for Symfony?
- Yes, the bundle is actively maintained for Laravel. While it originated in Symfony, it includes Laravel-specific components like Eloquent ORM support and fixes for Laravel compatibility (e.g., changelog issues #7973, #7963). Some Symfony-centric features may require minor adjustments, but core functionality works seamlessly.
- How do I install and set up API Platform Core in a Laravel project?
- Install via Composer: `composer require dunglas/api-bundle`. Then, configure the bundle in `config/app.php` under `providers` and `aliases`. Run `php artisan api:init` to generate a basic API resource. The bundle auto-discovers Eloquent models and generates REST/GraphQL endpoints from metadata (annotations, attributes, or YAML).
- Does API Platform Core support GraphQL in Laravel, and how does it compare to Laravel GraphQL packages?
- Yes, it generates GraphQL schemas alongside REST APIs from the same metadata. Unlike standalone Laravel GraphQL packages (e.g., `graphql-php`), API Platform Core provides a unified API layer with hypermedia (HATEOAS) support, OpenAPI docs, and shared business logic. It’s ideal if you need both REST and GraphQL from a single codebase.
- What Laravel versions does dunglas/api-bundle support, and are there breaking changes?
- The bundle supports Laravel 9.x and 10.x, with backward compatibility for most features. Breaking changes are documented in the [changelog](https://github.com/api-platform/core/blob/main/CHANGELOG.md) and typically involve Symfony dependency updates. Always check the latest version’s requirements before upgrading.
- How does API Platform Core handle authentication and authorization in Laravel?
- It integrates with Laravel’s built-in auth (e.g., Sanctum, Passport) via `ResourceAccessChecker`. For custom logic, use Symfony’s `Voter` or Laravel’s `Policy` classes. The bundle also supports OAuth2 and JWT out of the box, with configuration via YAML or annotations. Security policies can be applied per resource or operation.
- Can I customize API responses (e.g., add fields, modify serialization) without rewriting controllers?
- Absolutely. Use **DTOs (Data Transfer Objects)** to control serialization. Define custom serializers or override default behavior via `serializer` configuration in metadata (e.g., YAML or attributes). For dynamic fields, leverage `ApiProperty` or `ApiResource` extensions. No manual controller code is needed for basic customization.
- Does API Platform Core work with Laravel Scout for search-as-a-filter functionality?
- While not natively integrated, you can create a custom `SearchFilter` to bridge Scout with API Platform. The bundle’s filter system allows you to inject Scout queries into API requests (e.g., `?filter[search]=term`). This requires writing a custom filter class and registering it in the bundle’s configuration.
- How does API Platform Core handle performance in high-traffic Laravel APIs?
- Performance is optimized via JSON-LD streaming, Doctrine query caching (e.g., `IriFilter`), and OPcache. For Laravel, ensure you enable Redis for metadata caching (`api_platform.cache.redis`) and use Eloquent’s query caching. Load testing with tools like k6 or JMeter is recommended to identify bottlenecks in serialization or authentication.
- What alternatives exist for building REST/GraphQL APIs in Laravel if API Platform feels too complex?
- For simpler REST APIs, consider Laravel’s built-in API resources or packages like `spatie/laravel-api-resources`. For GraphQL, `graphql-php` or `rebing/laravel-graphql` are lightweight alternatives. If you need hypermedia or OpenAPI, `darkaonline/l5-swagger` (for Swagger) or `filp/whoops` (for error handling) can complement basic Laravel APIs.
- How do I migrate an existing Laravel API to API Platform Core without breaking functionality?
- Start by exposing one Eloquent model as an API resource (`php artisan api:init`). Gradually replace controllers with metadata-driven endpoints. Use the bundle’s `StateProvider` to handle custom logic (e.g., pre/post-processing). For auth, map Laravel’s middleware to API Platform’s `ResourceAccessChecker`. Test incrementally with tools like Postman or OpenAPI docs.