Primate: Restful API Library

dev-master 2016-04-30 10:20 UTC

This package is auto-updated.

Last update: 2022-11-29 02:11:20 UTC



Primate is a library that helps creating beautiful REST APIs.

It is heavily inspired by Stormpath's "The Fundamentals of REST API Design"

Using Primate

First you'll need to instantiate Primate, before your app can serve requests:

use Primate\Primate;

$primate = new Primate();
$primate->setProperty('tenant', 'joe');
$primate->setProperty('x', 'y');

You'll notice that the BaseUrl of your API is set on the Primate instance, in order to output correct urls.

Additionally, we're registering some arbitrary properties to define the context of the requests. You can use these properties later in order to fetch resources.

Resources and Collections

In Primate APIs, a client can work with Resources and Collections.

  • A Resource is simply an "object" in your application. For example, a User, a Product, etc.
  • A Collection is simply an array of Resources.


Each Resource is of a specified Type.

In Primate, you'll need to register one or more Types before you can use them. For example:

$repo = new MyContactRepository();
$type = new Type('contacts', $repo);

Each Type has a name and a repository. The Repository can be any class that's implementing the Primate\RepositoryInterface.

It's recommended to take your existing application repositories, and make them implement this interface.

For example:


namespace Example;

use Primate\RepositoryInterface;
use Primate\Resource;
use Primate\Primate;
use RuntimeException;

class PdoContactRepository implements RepositoryInterface
    public function __construct($pdo)
        $this->pdo = $pdo;
    // Implement your regular repository methods here...
    public function loadResourceCollection(Collection $collection, Primate $primate)
        foreach ($this->getAllContacts() as $contact) {
            $resource = new Resource($collection->getType(), $contact->getId());
    public function loadResources($resources, Primate $primate)
        foreach ($resources as $resource) {
            $contact = $this->findById($resource->getId());
            $resource->setProperty('name', $contact->getName());
            $resource->setProperty('gender', $contact->getGender());
            $phoneResource = $primate->createResource('phones', $contact->getPhoneId());
            $resource->setProperty('phone', $phoneResource);

Primate will call these methods to load resource data.

Calling Primate

You can make requests to Primate like this:

$data = $primate->getDataByPath($path, $expands);

The path variable is either:

  • /{typeName}: This will return a collection of all resources of that type
  • /{typeName}/{resourceId}: This will return the specific resource

Additionally, you can pass an array of expands.

Sub-resource expansion

When passing keys in the expands parameter, Primate will automatically expand the specified sub-resources.

For example, if your first response looks like this:

    "href": "",
    "name": "Alice",
    "gender": "Female",
    "phone": {
        "href": ""

You can ask Primate to 'expand the phone property:

    "href": "",
    "name": "Alice",
    "gender": "Female",
    "phone": {
        "href": "",
        "number": "+1 987654321",
        "type": "mobile"


MIT (see

Brought to you by the LinkORB Engineering team

Check out our other projects at

Btw, we're hiring!