php-open-source-saver/fractal
Fractal is a maintained fork of thephpleague/fractal for transforming complex data into consistent API output. Provides a presentation layer with transformers, type casting, relationship includes, custom serializers, and pagination support for JSON/YAML APIs.
IlluminatePaginatorAdapter, IlluminateSimplePaginatorAdapter), reducing friction with existing Laravel APIs.include=posts.comments), critical for performance in relational APIs.TransformResponse) for automatic serialization.| Risk Area | Mitigation Strategy |
|---|---|
| Learning Curve | Documentation from original thephpleague/fractal is comprehensive; namespace change is trivial. |
| Performance Overhead | Benchmark against raw json_encode for simple APIs; use caching (e.g., Fractal\Cache) for complex responses. |
| Namespace Migration | Automated IDE refactoring (League\Fractal → PHPOpenSourceSaver\Fractal) reduces manual effort. |
| Dependency Bloat | Lightweight (~1MB); no hard dependencies beyond PHP 8.1. |
| Long-Term Maintenance | Fork is actively maintained (last release: 2026-01-07); MIT license allows forks if needed. |
JsonApiSerializer for built-in compliance.json_encode)?Fractal\Cache\CacheInterface.TransformerAbstract.Fractal\Resource\Item/Collection alongside Laravel’s ApiResource.Route::middleware('transform') or app()->singleton('fractal', ...).PhalconFrameworkPaginatorAdapter or DoctrinePaginatorAdapter.links, meta, and included arrays.webonyx/graphql-php).| Phase | Action Items | Tools/Techniques |
|---|---|---|
| Assessment | Audit 3–5 API endpoints for nested data and inconsistent schemas. | Postman/Newman, PHPStan |
| Pilot | Replace 1–2 endpoints with Fractal transformers. | IDE refactoring, composer require |
| Core Integration | Standardize all API responses using Fractal; deprecate raw json_encode. |
Laravel middleware, app()->bind() |
| Testing | Validate schema stability (e.g., model field rename → no client impact). | Pact, Postman collections |
| Optimization | Profile performance; cache frequent responses with Fractal\Cache. |
Blackfire, Laravel Debugbar |
LengthAwarePaginator, SimplePaginatorSerializerInterface.return User::all() → return fractal->createData(User::all(), new UserTransformer).include params (e.g., /users?include=posts) for nested data.json_encode($model->toArray()) with fractal->toArray().Collection::setPaginator($paginator).JsonApiSerializer for standardized output.TransformResponse middleware to serialize all API responses.foreach loops for relationships.user_name → username in DB, but API still returns user_name).Accept: application/vnd.api.v1+json).Fractal\Cache (e.g., Redis) for expensive transformations.include is requested.json_encode for high-throughput APIs.with()) reduces N+1 queries but may increase initial load time.| Scenario | Mitigation |
|---|---|
| Transformer Bug | Unit test transformers; use Fractal\Test\Mock for isolated testing. |
| Circular References | Configure JsonApiSerializer to handle cycles or use maxDepth. |
| Pagination Errors | Validate paginator adapters (e.g., DoctrinePaginatorAdapter). |
| Schema Drift | Enforce API contracts (e.g., OpenAPI) and use Fractal to shield clients. |
| PHP 8.1+ Dependency | Upgrade PHP or fork the package (MIT license allows modifications). |
How can I help you explore Laravel packages today?