violines/rest-bundle is a Symfony Bundle to create REST APIs. It focusses on HTTP standards and integrates the symfony/serializer and symfony/validator.
- 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
- Symfony 5.4 + 6
- PHP 8 + 8.1
modern architectures that apply Domain Driven Design principles, hexagonal architecture or similar concepts.
composer require violines/rest-bundle
- Create any PHP class (Entity, DTO, Command, Query, etc) and add the
#[HttpApi]
attribute or@HttpApi
annotation - Use any property attributes/annotations from symfony/serializer or symfony/validator
- Declare this PHP class as type of a controller argument
- Return an instance of this PHP class in the controller
Find a sample of usage under: https://github.com/violines/rest-bundle-showcase.
<?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);
}
}
For more details please check violines/rest-bundle Wiki.
- copy docker/.env.dist to docker/.env and adjust to your needs
- cd docker/
- pull latest image(s): docker-compose pull
- create the container(s): docker-compose up -d
- run tests with
docker-compose exec php80 composer run test