RAPL (RESTful API Persistence Layer) is a RESTful variant of Doctrine's ORM. It implements the same interfaces, but allows you to store and retrieve entities from a remote (RESTful) API instead of from the database.
- RAPL abstracts the REST architecture, the HTTP protocol and the serialization of objects for you. All you have to do is to create your entity classes and to map them to the API (using mapping configuration).
- If you are maintaining an API and you want to provide a client library for it, you can simply build it on top of RAPL.
RAPL can be installed using Composer:
composer require rapl/rapl
This will add RAPL to the dependency list of your main project's composer.json.
<?php
require_once 'vendor/autoload.php';
$connection = new \RAPL\RAPL\Connection\Connection('http://example.com/api/');
$connection->addSubscriber(new \Your\Authentication\Subscriber());
$configuration = new \RAPL\RAPL\Configuration();
$paths = array(__DIR__ . '/config');
$driver = new \RAPL\RAPL\Mapping\Driver\YamlDriver($paths, '.rapl.yml');
$configuration->setMetadataDriver($driver);
$manager = new \RAPL\RAPL\EntityManager($connection, $configuration);
Once you have set everything up correctly, you can start using RAPL. This will feel very familiar if you have worked with Doctrine before.
$repository = $manager->getRepository('Your\Entity\Class');
// Get entity with id 3
$entity = $repository->find(3);
// Or get all of them
$entities = $repository->findAll();