All Projects → gdebrauwer → Laravel Hateoas

gdebrauwer / Laravel Hateoas

Licence: mit
Expose the authorization logic of your REST API using HATEOAS links

Projects that are alternatives of or similar to Laravel Hateoas

Api Restful Con Laravel Guia Definitiva
Repositorio para el código base del curso "API RESTful con Laravel - Guía Definitiva"
Stars: ✭ 95 (-18.1%)
Mutual labels:  api, rest-api, laravel
Larapi
An API-friendly fork of Laravel. Authentication, error handling, resource filtering, sorting, pagination and much more included
Stars: ✭ 397 (+242.24%)
Mutual labels:  api, rest-api, laravel
Restful Api With Laravel Definitive Guide
Repository with the base code for the course "RESTful API with Laravel - Definitive-Guide"
Stars: ✭ 156 (+34.48%)
Mutual labels:  api, rest-api, laravel
Laravel Api Handler
Package providing helper functions for a Laravel REST-API
Stars: ✭ 150 (+29.31%)
Mutual labels:  api, rest-api, laravel
Laravel Compass
A REST client inside your Laravel app
Stars: ✭ 1,002 (+763.79%)
Mutual labels:  api, rest-api, laravel
Lumen Microservice
Lumen on Docker - Skeleton project with Nginx, MySQL & PHP 7 | Aws ECS, Google Kubernates, Azure Container Engine
Stars: ✭ 183 (+57.76%)
Mutual labels:  api, rest-api, laravel
Jikan Rest
The REST API for Jikan
Stars: ✭ 200 (+72.41%)
Mutual labels:  api, rest-api, laravel
Laravel Api Response Builder
Builds nice, normalized and easy to consume Laravel REST API JSON responses.
Stars: ✭ 433 (+273.28%)
Mutual labels:  api, rest-api, laravel
Binding Of Isaac Api
A RESTful API for the Binding of Isaac game series
Stars: ✭ 11 (-90.52%)
Mutual labels:  api, rest-api, laravel
Laravel Orion
The simplest way to create REST API with Laravel
Stars: ✭ 481 (+314.66%)
Mutual labels:  api, rest-api, laravel
Laravel Api Boilerplate
A Boilerplate Project For Laravel API's (NOT MAINTAINED)
Stars: ✭ 113 (-2.59%)
Mutual labels:  api, rest-api, laravel
Laravel Woocommerce
WooCommerce Rest API for Laravel
Stars: ✭ 86 (-25.86%)
Mutual labels:  api, rest-api, laravel
Overwatch Api
A RESTful API for the Overwatch Game
Stars: ✭ 112 (-3.45%)
Mutual labels:  api, rest-api, laravel
Tiledesk Server
Tiledesk server. Tiledesk is an Open Source Live Chat platform written in NodeJs and MongoDB
Stars: ✭ 94 (-18.97%)
Mutual labels:  api, rest-api
Chi
lightweight, idiomatic and composable router for building Go HTTP services
Stars: ✭ 10,581 (+9021.55%)
Mutual labels:  api, rest-api
Http restful api
整理HTTP后台端的RESTful API方面的知识
Stars: ✭ 94 (-18.97%)
Mutual labels:  api, rest-api
Laravel Rest Api
Powerful RestAPI plugin for Laravel
Stars: ✭ 90 (-22.41%)
Mutual labels:  rest-api, laravel
Open Semantic Entity Search Api
Open Source REST API for named entity extraction, named entity linking, named entity disambiguation, recommendation & reconciliation of entities like persons, organizations and places for (semi)automatic semantic tagging & analysis of documents by linked data knowledge graph like SKOS thesaurus, RDF ontology, database(s) or list(s) of names
Stars: ✭ 98 (-15.52%)
Mutual labels:  api, rest-api
Laradoo
Odoo ERP API for Laravel
Stars: ✭ 100 (-13.79%)
Mutual labels:  api, laravel
Cloudbookmark
云书签
Stars: ✭ 106 (-8.62%)
Mutual labels:  rest-api, laravel

Laravel HATEOAS

Latest Version on Packagist GitHub 'Run Tests' Workflow Status Total Downloads

HATEOAS allows you to expose the authorization logic of your REST API. This package makes it easy to add HATEOAS links to your Laravel API resources.

Each resource has its HATEOAS links, and only the accessible links per resource are returned. If a link is not available on a resource, then the clients of your API can disable functionality linked to that HATEOAS link.

By default an array of links, in the following format, will be added to the JSON of a Laravel API resource:

{
    "data": [
        {
            "id": 1,
            "text": "Hello world!",
            "_links": [
                {
                    "rel": "self",
                    "type": "GET",
                    "href": "http://localhost/message/1"
                },
                {
                    "rel": "delete",
                    "type": "DELETE",
                    "href": "http://localhost/message/1"
                }
            ]
        }
    ]
}

Installation

You can install the package via composer:

composer require gdebrauwer/laravel-hateoas

Usage

You can create a new HATEOAS class for a model using the following artisan command:

php artisan make:hateoas MessageHateoas --model=Message

In the created class you can define public methods that will be used to generate the links. A method should either return a link or null.

class MessageHateoas
{
    use CreatesLinks;

    /**
     * Get the HATEOAS link to view the message.
     *
     * @param \App\Message $message
     *
     * @return null|\GDebrauwer\Hateoas\Link
     */
    public function self(Message $message)
    {
        if (! auth()->user()->can('view', $message)) {
            return;
        }

        return $this->link('message.show', ['message' => $message]);
    }

    /**
     * Get the HATEOAS link to delete the message.
     *
     * @param \App\Message $message
     *
     * @return null|\GDebrauwer\Hateoas\Link
     */
    public function delete(Message $message)
    {
        if (! auth()->user()->can('delete', $message)) {
            return $this->link('message.archive', ['message' => $message]);
        }

        return $this->link('message.destroy', ['message' => $message]);
    }
}

To add the links to an API resource, you have to add the HasLinks trait and use the $this->links() method. The HATEOAS class will be automatically discovered.

class MessageResource extends JsonResource
{
    use HasLinks;

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'text' => $this->text,
            '_links' => $this->links(),
        ];
    }
}

Customization

Formatting

You can customize the JSON links formatting by providing a formatter class that implements the Formatter interface to the formatLinksUsing method. If the code to format the links is pretty small or you don't want to create a separate formatter class for it, you also have the option to provide a formatting callback function to the formatLinksUsing method.

use GDebrauwer\Hateoas\Hateoas;
use GDebrauwer\Hateoas\LinkCollection;

// Provide your own Formatter class ...
Hateoas::formatLinksUsing(CustomFormatter::class);

// ... Or provide a callback
Hateoas::formatLinksUsing(function (LinkCollection $links) {
    // return array based on links
});

HATEOAS class discovery

By default, the HATEOAS classes of models will be auto-discovered. Specifically, the HATEOAS classes must be in a Hateoas directory below the directory that contains the models. If you would like to provide your own HATEOAS class discovery logic, you can register a custom callback:

use GDebrauwer\Hateoas\Hateoas;

Hateoas::guessHateoasClassNameUsing(function (string $class) {
    // return a HATEOAS class name
});

Testing

composer test

Linting

composer lint

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].