A framework agnostic PHP library based on fractal to help you craft beautiful REST APIs.
The main goal is to provide an easy to use and powerful REST API library that seamlessly integrates with modern frameworks.
The paradigm reads as follow:
composer require wizards/rest-api
The library's conventions are based on the jsonapi ones.
The RestQueryParser will expect those query parameters:
Collection
sort
name
to sort by ascending name-name
to sort by descending name?sort=-date
filter
to filter resources by values.
?filter[name]=dupont,dupond&filter[surname]=thomas
filteroperator
=
to something else. <
, >
, <=
, >=
, !=
, in
.?filter[age]=18&filteroperator[age]=>=
include
to include relationships data.
/books?include=author,editor
limit
: how many results you want to see by page.page
: the page number. Starts at 1.Single resource
include
to include relationships data. Example: /books/1?include=author,editor
<?php
namespace App\Controller;
use Psr\Http\Message\ServerRequestInterface;
use WizardsRest\ObjectManager\ArrayObjectManager;
use WizardsRest\CollectionManager;
use WizardsRest\Provider;
use WizardsRest\Serializer;
use WizardsRest\ObjectReader\ArrayReader;
use WizardsRest\Paginator\ArrayPagerfantaPaginator;
use Symfony\Component\Routing\RouterInterface;
use WizardsRest\Transformer\ArrayTransformer;
Class BookController {
private $source = [
['name' => 'Book 1', 'author' => 'Author 1', 'editor' => 'Editor 1'],
['name' => 'Book 2', 'author' => 'Author 2', 'editor' => 'Editor 2'],
];
// Books controller. Somehow, this is called
public function getBooks(RouterInterface $router, ServerRequestInterface $request) {
// Fetch
$objectManager = new ArrayObjectManager();
$paginator = new ArrayPagerfantaPaginator($router);
$collectionManager = new CollectionManager($paginator, $objectManager);
$collection = $collectionManager->getPaginatedCollection($this->source, $request);
// Transform
$fractalManager = new \League\Fractal\Manager();
$reader = new ArrayReader();
$provider = new Provider(new ArrayTransformer(), $fractalManager, $reader);
$resource = $provider->transform($collection, $request, null, 'books');
// Serialize
$serializer = new Serializer($fractalManager, 'https://mysite.com');
return $serializer->serialize($resource, Serializer::SPEC_DATA_ARRAY, Serializer::FORMAT_ARRAY);
}
}
See the documentation on Wizards Technologies' REST API Bundle
We are actively looking for laravel developers to support it !