violines / rest-bundle
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: 184
Dependents: 0
Suggesters: 0
Security: 0
Stars: 35
Watchers: 4
Forks: 1
Open Issues: 2
Type:symfony-bundle
Requires
- php: ^8.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- symfony/config: ^5.4 || ^6.0
- symfony/contracts: ^2.5 || ^3.0
- symfony/dependency-injection: ^5.4 || ^6.0
- symfony/http-foundation: ^5.4 || ^6.0
- symfony/http-kernel: ^5.4 || ^6.0
- symfony/serializer: ^5.4 || ^6.0
- symfony/validator: ^5.4 || ^6.0
Requires (Dev)
- doctrine/annotations: ^1.11
- friendsofphp/php-cs-fixer: ^2.16
- infection/infection: ^0.25.6
- matthiasnoback/symfony-dependency-injection-test: ^4.1
- mikey179/vfsstream: ^1.6
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.3.0
- rector/rector: ^0.12.13
- symfony/filesystem: ^5.4 || ^6.0
- symfony/framework-bundle: ^5.4 || ^6.0
- symfony/property-access: ^5.4 || ^6.0
- vimeo/psalm: ^4.0
Suggests
- doctrine/annotations: ^1.11
- symfony/property-access: ^5.4 || ^6.0
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.
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?
- 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
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
- 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