2017 © Pedro Peláez
 

library restful

Create RESTful API responses with ease

image

lukaskorl/restful

Create RESTful API responses with ease

  • Wednesday, December 10, 2014
  • by lukaskorl
  • Repository
  • 1 Watchers
  • 8 Stars
  • 134 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Create RESTful API responses with ease

This package enables you to create RESTful API responses without thinking about the specifics of the underlying HTTP layer. You provide data and the package will take care of output format and status codes. In case of an application error also RESTful error reponses can be created. The package is optimized for the use with the Laravel framework. This documentation heavily focuses on the use within the Laravel framework., (*1)

Basic Usage

RESTful provides two separate functions for returning a collection or a single entity., (*2)

  • Entity: If you follow the REST conventions an entity is a single entry of a REST resource. In most cases an entity is returned whenever the URI ends with a resource ID.
  • Collection: A collection contains a set of entities

Returning a single entity

To return a single entity you can call the entity method on the Restful facade and provide anything that implements the ArrayAccess interface., (*3)

$response = Restful::entity([
    'name' => 'Dexter',
    'age' => 1
]);

RESTful will return a \Illuminate\Http\Response which directly extends the \Symfony\Component\HttpFoundation\Response. If you work with the Laravel 4 framework you can directly return the response object., (*4)

If you use the response object correctly the application will return:, (*5)

Content-Type:application/json

{
    name: "Dexter",
    age: 1
}

Returning a collection

Analogous to the entity method the collection method accepts a collection of entities., (*6)

$response = Restful::collection([
    [
        'name' => 'Dexter',
        'age' => 1
    ],  
    [
        'name' => 'Dori',
        'age' => 1
    ],  
]);

The collection may be of the type:, (*7)

  • array: Use an indexed array (as a collection) containing associative arrays (as entities)
  • Illuminate\Support\Collection: Basically the same as providing an array.
  • \Illuminate\Pagination\Paginator: When a paginator is provided RESTful will automatically include metadata about the pagination in the response.

The response will look like:, (*8)

Content-Type:application/json

{
    data: [
        {
            name: "Dexter",
            age: 1
        },
        {
            name: "Dori",
            age: 1
        }
    ]
}

Be aware that collections are wrapped into a data attribute. This is necessary because collections may also return metadata. This metadata may include pagination information., (*9)

Usage in Laravel 4

When using RESTful in Laravel 4 you can leverage the power of Eloquent to drive your responses. Consider the following lines as result of any controller action., (*10)

return Restful::entity(Dogs::find(1));
return Restful::collection(Dogs::all());
return Restful::collection(Dogs::paginate(3));

Advanced API

Forcing output format

If you wan't to force an output format you can use the corresponding method and chain it with the output you want to serve., (*11)

return Restful::serialized()->collection(Dogs::all());
return Restful::php()->collection(Dogs::all());
return Restful::json()->collection(Dogs::all());
return Restful::jsonp('myFunction')->collection(Dogs::all());
return Restful::yaml()->collection(Dogs::all());
return Restful::xml()->collection(Dogs::all());

Content-Type header will be set automatically according to the specified format., (*12)

Errors

Also common errors can easily be handled with RESTful., (*13)

return Restful::code(404)->error("Ups");
return Restful::forbidden();
return Restful::unauthorized();
return Restful::missing();

Installation

Within Laravel 4 framework

To install RESTful simply add it as a requirement to the projects composer.json:, (*14)

"require": {
    "lukaskorl/restful": "dev-develop"
}

or run composer require lukaskorl/restful., (*15)

Word of caution: RESTful is currently under heavy development. I will release a stable version 1.0.0 soon. Until then the interface may change. I do not recommend to use RESTful as a dev-develop dependency in production code., (*16)

Vanilla PHP

If you want to use RESTful in any other PHP application install it via composer as above. To use RESTful you will have to add a few more lines of code see the example code below:, (*17)

<?php
require("vendor/autoload.php");
use \Lukaskorl\Restful\Facades\Vanilla as Restful;

Restful::entity(array( 'name' => 'Dexter' ))->send();

In comparison to using RESTful in a framework you don't need to return the result of the call but to directly render+send the output back to the use., (*18)

License

RESTful is open-source software licensed under the MIT license, (*19)

The Versions

10/12 2014
10/12 2014