fractal
A presentation and transformation layer for complex PHP API output
Repository Health
Technical Analysis
Fractal provides a presentation and transformation layer for complex data output, the kind found in RESTful APIs, and works especially well with JSON. Think of it as a view layer for your API responses: instead of grabbing rows from the database and passing them straight to json_encode(), you route them through transformers that give you consistent, well-typed, versionable output.
Part of The PHP League, Fractal creates a protective shield between your source data and what clients see, handles embedding of related resources, supports pagination, and works with output standards like JSON:API and HAL as well as fully custom serializers.
What You Get
- Transformer classes that map source records to clean, consistent output arrays
- Resource types for single items, collections, primitives, and null resources
- Pluggable serializers including a JSON:API serializer and custom serializer support
- Includes/embedding for nesting related resources and paginator adapters for common frameworks
Common Use Cases
- Building consistent, versionable JSON responses for public REST APIs
- Decoupling API output from database schema so internal changes don’t break clients
- Embedding related resources on demand and paginating large collections
Under The Hood
Architecture - The Manager is the entry point: you create a resource (Item, Collection, Primitive, or NullResource from src/Resource) bound to a Transformer, then call createData() to build a Scope. Scopes handle nesting and the requested includes tree, while a Serializer (src/Serializer) turns the transformed data into the final array shape. A ScopeFactory decouples scope creation, and Pagination adapters normalize paginators from several frameworks behind a PaginatorInterface.
Tech Stack - Pure PHP 7.4+ with no required runtime dependencies; framework paginators (Illuminate, Doctrine, Pagerfanta, Laminas) are optional dev/suggested packages. Distributed via Composer under the League\Fractal namespace, with php-cs-fixer, PHPStan, and Psalm in the dev toolchain.
Code Quality - A mature, well-structured codebase built on interfaces (ResourceInterface, SerializerInterface, PaginatorInterface) that keep the design extensible. Roughly 35 test files with PHPUnit and Mockery cover the core, though recent commit activity is low as the library is stable and feature-complete.
API Design - The developer experience is clean and well documented on fractal.thephpleague.com: define a transformer, wrap data in a resource, choose a serializer, and read the output. Include definitions and default includes make embedding intuitive, and swapping serializers to target JSON:API or a custom format is a one-line change.
Used by 2 apps in this directory
Invoice Ninja
Invoicing Finance · Project Management
Self-hostable invoicing, time-tracking, and multi-gateway payment platform for freelancers and small businesses, with built-in e-invoicing compliance for EU and global standards.
Krayin CRM
Ecommerce · CRM
Free, open-source Laravel & Vue.js CRM for SMEs and enterprises to manage the complete customer lifecycle.