Laravel HATEOAS Package

Originally published by Paul Redmond at https://laravel-news.com

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. 

The package defines an artisan command for creating a new HATEOAS class, which contains methods that will generate links JSON responses:

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]);
    }
}

Then in your Laravel resources class, you can include the links using this package’s HasLinks trait:

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(),
        ];
    }
}

 Finally, the result of the above example might look like the following JSON response:

{
    "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"
                }
            ]
        }
    ]
}

 If you are not familiar with HATEOAS, it stands for Hypermedia As The Engine of Application State. You can get a pretty good overview of HATEOAS from the HATEOAS – Wikipedia page.

You can learn more about this package, get full installation instructions, and view the source code on GitHub at gdebrauwer/laravel-hateoas.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

PHP with Laravel for beginners - Become a Master in Laravel

Projects in Laravel: Learn Laravel Building 10 Projects

Laravel for RESTful: Build Your RESTful API with Laravel

Fullstack Web Development With Laravel and Vue.js

Laravel 5.8 Tutorial for Beginners

Laravel 5.8 Ajax CRUD tutorial using Datatable JS

Laravel 5.8 Tutorial from Scratch for Beginners

Build RESTful API In Laravel 5.8 Example

Login with Google in Laravel 5.8 App using Socialite Package

Laravel PHP Framework Tutorial - Full Course for Beginners (2019)

#laravel #php #rest #api

Laravel HATEOAS Package
26.55 GEEK