wizards/rest-bundle

Integration of php-rest-api into Symfony.

Installs: 4 913

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 4

Open Issues: 1

Type:symfony-bundle

1.0-beta5 2021-01-17 22:19 UTC

README

Symfony bundle for wizards/php-rest-api

Build Status

Helps you create a REST API in an expressive and streamlined way. It will help you conceive mature and discoverable APIs, thanks to the jsonapi specification. Regardless of the output serialization, the request format will be the same. Have a look at http://github.com/wizardstechnologies/php-rest-api for further documentation and explanations. You can find an example project on https://github.com/BigZ/promoteapi

Requirements

symfony >= 4.4
php >= 7.3

Installation

composer require wizards/rest-bundle

Configuration

Create a configuration file with the following values:

# config/bundles/wizards_rest.yaml
wizards_rest:
	data_source: orm|array # Choose between ORM and Array for your data source. More will be added soon
	reader: annotation|array
	format: jsonapi|array
	base_url: your_url

Usage

Create a REST API the easy and configurable way !

This bundle ease the use of wizard's php rest api, and provide some extra goodies for symfony:

If you use symfony flex, those services will be automatically registered.

To serialize a single resource, just return the object from a controller:

public function getArtistAction(string $id, EntityManagerInterface $entityManager)
{
    try {
        $artist = $entityManager->find(Artist::class, $id);
    } catch (\Exception $exception) {
        throw new NotFoundHttpException('Artist not found.');
    }

    return $artist;
}

Note that we don't use the param injector for the entity as we want to be able to dispatch an error ourselves so it is properly formatted.

To Serialize a collection, use the collectionManager

public function getArtistsAction(CollectionManager $collectionManager, ServerRequestInterface $request)
{
    return $collectionManager->getPaginatedCollection(Artist::class, $request);
}

To deserialize input, use the json trait

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Wizards\RestBundle\Controller\JsonControllerTrait;

class ArtistController extends AbstractController
{
    use JsonControllerTrait;
	public function postArtistAction(Request $request, EntityManagerInterface $entityManager)
    {
        $artist = new Artist();
        $form = $this->createForm(ArtistType::class, $artist);
        $this->handleJsonForm($form, $request);

        if (!$form->isValid()) {
            $this->throwRestErrorFromForm($form);
        }

        $entityManager->persist($artist);
        $entityManager->flush();

        return $artist;
    }
}