simon-schubert/terry-api

This package is abandoned and no longer maintained. The author suggests using the violines/rest-bundle package instead.

violines/rest-bundle is a Symfony Bundle to create REST APIs. It focusses on HTTP standards and integrates the symfony/serializer and symfony/validator.

Installs: 131

Dependents: 0

Suggesters: 0

Security: 0

Stars: 35

Watchers: 4

Forks: 2

Open Issues: 2

Type:symfony-bundle

v0.8.1 2022-03-09 13:05 UTC

This package is auto-updated.

Last update: 2022-03-09 13:08:09 UTC


README

violines/rest-bundle is a Symfony Bundle to create REST APIs. It focusses on HTTP standards and integrates the symfony/serializer and symfony/validator.

build Code Coverage Mutation testing badge type coverage Technical Debt Software License Wiki Docs

Features

  • Request body or query string to object conversion
  • Response building from objects
  • Configurable content negotiation
  • Events to control symfony/serializer context
  • Integration of symfony/validator
  • Error Handling
  • Optional Logging

Compatible with...

  • Symfony 5.4 + 6
  • PHP 8 + 8.1

Designed for...

modern architectures that apply Domain Driven Design principles, hexagonal architecture or similar concepts.

Install

composer require violines/rest-bundle

How does it work?

  1. Create any PHP class (Entity, DTO, Command, Query, etc) and add the #[HttpApi] attribute or @HttpApi annotation
  2. Use any property attributes/annotations from symfony/serializer or symfony/validator
  3. Declare this PHP class as type of a controller argument
  4. Return an instance of this PHP class in the controller

Show Case

Find a sample of usage under: https://github.com/violines/rest-bundle-showcase.

Example

<?php

declare(strict_types=1);

namespace App\Entity;

#[Violines\RestBundle\HttpApi\HttpApi]
final class Order
{
    public $amount;
    public $articles;
}

// Or use Doctrine Annotations (requires separate install):
// In order to use Annotations you have to install `doctrine/annotations` via `composer require doctrine/annotations`

/**
 * @Violines\RestBundle\HttpApi\HttpApi
 */
final class Order {}
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Exception\AuthenticationFailedException;
use App\Entity\Order;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class OrderController
{
    /**
     * @return Order[]
     */
    #[Route('/orders', methods: ['GET'], name: 'find_orders')]
    public function findOrders(): array
    {
        return $this->orderRepository->findOrders();
    }

    #[Route('/order/{id}', methods: ['GET'], name: 'find_order')]
    public function findOrder(int $id): Order
    {
        $order = $this->orderRepository->find($id);

        if (null === $order) {
            throw NotFoundException::id($id);
        }

        return $order;
    }

    /**
     * @param Order[] $orders
     */
    #[Route('/orders/create', methods: ['POST'], name: 'create_orders')]
    public function createOrders(Order ...$orders): Response
    {
        $this->orderRepository->createOrders($orders);

        return new Response(null, Response::HTTP_CREATED);
    }

    #[Route('/order/create', methods: ['POST'], name: 'create_order')]
    public function createOrder(Order $order): Response
    {
        $this->orderRepository->create($order);

        return new Response(null, Response::HTTP_CREATED);
    }
}

Wiki

For more details please check violines/rest-bundle Wiki.

Development setup

  1. copy docker/.env.dist to docker/.env and adjust to your needs
  2. cd docker/
  3. pull latest image(s): docker-compose pull
  4. create the container(s): docker-compose up -d
  5. run tests with docker-compose exec php80 composer run test