egeriis / laravel-jsonapi

Make it a breeze to create a jsonapi.org compliant APIs with Laravel 5.
MIT License
146 stars 27 forks source link

!!! Project abandoned. See cloudcreativity/laravel-json-api for a great alternative.

JSON API helpers for Laravel 5

Build Status

Make it a breeze to create a jsonapi.org compliant API with Laravel 5.

This library strives to be up to date with the latest JSON API updates—as the spec is still a work in progress. If you notice that something is missing, please contribute!

Installation

  1. Add echo-it/laravel-jsonapi to your composer.json dependency list (version 2.0.0 at the minimum for laravel 5 support)

  2. Run composer update.

Requirements

Using laravel-jsonapi

This library is made with the concept of exposing models in mind, as found in the RESTful API approach.

In few steps you can expose your models:

  1. Create a route to direct the requests

    In this example, we use a generic route for all models and HTTP methods:

    Route::any('{model}/{id?}', 'ApiController@handleRequest');
  2. Create your controller to handle the request

    Your controller is responsible to handling input, instantiating a handler class and returning the response.

    
    <?php namespace App\Http\Controllers;

use EchoIt\JsonApi\Request as ApiRequest; use EchoIt\JsonApi\ErrorResponse as ApiErrorResponse; use EchoIt\JsonApi\Exception as ApiException; use Request;

class ApiController extends Controller { public function handleRequest($modelName, $id = null) { /**

  1. Create a handler for your model

    A handler is responsible for exposing a single model.

    In this example we have create a handler which supports the following requests:

    • GET /users (ie. handleGet function)
    • GET /users/[id] (ie. handleGet function)
    • PUT /users/[id] (ie. handlePut function)

    Requests are automatically routed to appropriate handle functions.

    
    <?php namespace App\Handlers;

use Symfony\Component\HttpFoundation\Response; use App\Models\User;

use EchoIt\JsonApi\Exception as ApiException; use EchoIt\JsonApi\Request as ApiRequest; use EchoIt\JsonApi\Handler as ApiHandler; use Request;

/**

Current features

According to jsonapi.org:

The features in the Handler class are each in their own function (eg. handlePaginationRequest, handleSortRequest, etc.), so you can easily override them with your own behaviour if desired.

Wishlist