Skip to content

A set of tools to simplify working with API Platform in your projects. Comes with guides, patterns, and practical examples for building API Platform-based projects.

Notifications You must be signed in to change notification settings

rekalogika/api-lite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

27f4a7a · Jan 14, 2025

History

52 Commits
Sep 16, 2024
Sep 4, 2024
Sep 4, 2024
Sep 16, 2024
Jan 3, 2025
Feb 28, 2024
Feb 28, 2024
Feb 28, 2024
Feb 28, 2024
Sep 4, 2024
Jan 14, 2025
Jun 11, 2024
Feb 28, 2024
Jan 14, 2025
Sep 1, 2024
Oct 30, 2024
Apr 21, 2024
Sep 1, 2024

Repository files navigation

rekalogika/api-lite

A set of tools to simplify working with API Platform in your projects. Comes with guides, patterns, and practical examples for building API Platform-based projects.

Apply existing knowledge, experience, and patterns of working with Symfony controllers to API Platform state providers and processors. Decouple your persistence layer from the API frontend layer. Apply Domain-Driven Design (DDD) principles to your API Platform-based projects, as well as other architectural patterns and best practices, including SOLID, onion architecture, clean architecture, hexagonal architecture, and others.

'Lite' means we are refraining from using all of API Platform's features and automations in favor of better readability, simplicity, and flexibility.

Full documentation is available at rekalogika.dev/api-lite.

Motivation

API Platform documentation encourages developers to use plain old PHP objects (POPOs) or data transfer objects (DTOs) as the models for API communication, instead of using domain entities directly for this purpose. But it does not establish a practical working patterns for that approach.

Practically all the examples and demos we find on the Internet still attach ApiResource to Doctrine entities.

Sometimes API Platform can feel very rigid. It can be difficult to figure out how to accomplish things outside its conventions. There are ways around any problem, just not always immediately obvious. It can feel like that we just want to express what we need by writing a PHP code, not by figuring out the correct combination of attributes to use.

Those coming from Symfony controllers might find API Platform's approach very different, but it does not have to be.

Installation

composer require rekalogika/api-lite

Synopsis

use Doctrine\Common\Collections\Collection;
use Rekalogika\ApiLite\State\AbstractProvider;
use Rekalogika\Mapper\CollectionInterface;

#[ApiResource(
    shortName: 'Book',
    operations: [
        new Get(
            uriTemplate: '/books/{id}',
            provider: BookProvider::class
        ),
    ]
)]
class BookDto
{
    public ?Uuid $id = null;
    public ?string $title = null;
    public ?string $description = null;

    /**
     * @var ?CollectionInterface<int,ReviewDto>
     */
    public ?CollectionInterface $reviews = null;
}

/**
 * @extends AbstractProvider<BookDto>
 */
class BookProvider extends AbstractProvider
{
    public function __construct(
        private BookRepository $bookRepository
    ) {
    }

    public function provide(
        Operation $operation,
        array $uriVariables = [],
        array $context = []
    ): object|array|null {
        $book = $this->bookRepository
            ->find($uriVariables['id'] ?? null)
            ?? throw new NotFoundException('Book not found');

        $this->denyAccessUnlessGranted('view', $book);

        return $this->map($book, BookDto::class);
    }
}

To-Do List

  • Figure out & implement filtering.

Documentation

rekalogika.dev/api-lite

License

MIT

About

A set of tools to simplify working with API Platform in your projects. Comes with guides, patterns, and practical examples for building API Platform-based projects.

Resources

Stars

Watchers

Forks

Packages

No packages published