2017 © Pedro Peláez
 

symfony-bundle rest-bundle

This Bundle wraps the innmind rest server library

image

innmind/rest-bundle

This Bundle wraps the innmind rest server library

  • Friday, January 8, 2016
  • by Baptouuuu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 20,120 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 20 Versions
  • 3 % Grown

The README.md

RestBundle

Scrutinizer Code Quality Code Coverage Build Status, (*1)

SensioLabsInsight, (*2)

Wrapper for the innmind/rest-server library allowing you to easily expose a REST L3 API., (*3)

This bundle offer a complete integration of the library in a symfony project. It also add a new feature called server capabilities; put simply, it expose a route OPTIONS * that will output the list of routes exposed via this bundle. The goal being to allow client discovery, you could imagine a REST client that could prefetch all the resources definitions so it could know in advance if the resources it will try to send match the definitions., (*4)

Installation

composer require innmind/rest-bundle

Enable the bundle by adding the following line in your app/AppKernel.php of your project:, (*5)

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Innmind\RestBundle\InnmindRestBundle,
        );
        // ...
    }
    // ...
}

Then specify your resources in the configuration under:, (*6)

innmind_rest:
    server:
        collections: [] #same configuration as the rest server library
        prefix: /api/prefix #optional

Server

Storage

To define a storage you can create a service having either innmind_rest.server.storage.abstract.doctrine or innmind_rest.server.storage.abstract.neo4j as parent. Then you need to specify the first argument to construct the service, being an instance of an entity manager (a doctrine or neo4j one); and flag the service with the tag innmind_rest.server.storage, the bundle will look for the attribute alias on this tag to use as reference afterward (name used to specify storage on your resources)., (*7)

Formats

As allowed formats are handled via encoders, you declare new ones with a tag on the encoder service you want to add., (*8)

Example of the built-in json format:, (*9)

innmind_rest.server.serializer.encoder.json:
    class: Innmind\Rest\Server\Serializer\Encoder\JsonEncoder
    tags:
        - { name: serializer.encoder }
        - { name: innmind_rest.server.format, format: json, mime: application/json, priority: 10 }

Events

In most cases the only event you'll want to alter will be KernelEvents::RESPONSE or Events::{STORAGE}_READ_QUERY_BUILDER (STORAGE can be DOCTRINE or NEO4J) to add restriction on the query like for example the user being connected., (*10)

You can look at Events.php to review all the events you have at your disposition., (*11)

Client

To consume an API you need to use the innmind_rest.client service. Examples of usages:, (*12)

use Innmind\Rest\Client\HttpResource;

$client = $container->get('innmind_rest.client');

$resources = $client->getServer('http://example.com')->read('some_resource');
$resource = $client->getServer('http://example.com')->read('some_resource', 42);

$toCreate = new HttpResource;
$toCreate->set('someProperty', 'value');
$client->getServer('http://example.com')->create('some_resource', $toCreate);

$toUpdate = new HttpResource;
$toUpdate
    ->set('all', 'properties')
    ->set('must', 'be set');
$client->getServer('http://example.com')->update('some_resource', 42, $toUpdate);

$client->getServer('http://example.com')->remove('some_resource', 42);

You can use $server->getResources() to view all the resources exposed by the server API; it will return an associative array with the names as keys and the definitions as values., (*13)

The Versions