PHPackages                             hpolthof/laravel-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. hpolthof/laravel-api

ActiveLibrary[HTTP &amp; Networking](/categories/http)

hpolthof/laravel-api
====================

An API rendering package for Laravel 5

v0.1.4(9y ago)326GPL2PHPPHP &gt;=5.4.0

Since Nov 28Pushed 9y ago1 watchersCompare

[ Source](https://github.com/hpolthof/laravel-api)[ Packagist](https://packagist.org/packages/hpolthof/laravel-api)[ RSS](/packages/hpolthof-laravel-api/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)DependenciesVersions (6)Used By (0)

Uniform API rendering for Laravel 5
===================================

[](#uniform-api-rendering-for-laravel-5)

[![Latest Stable Version](https://camo.githubusercontent.com/81f1761391740efd658fd00b4cdd7ff6093bcd96b77e13eddd6708fefe39af6e/68747470733a2f2f706f7365722e707567782e6f72672f68706f6c74686f662f6c61726176656c2d6170692f762f737461626c652e737667)](https://packagist.org/packages/hpolthof/laravel-api)[![License](https://camo.githubusercontent.com/435742db845a2858f64e862e5d12a501e421d5c41f6fdf6bed365754eeb8ad66/68747470733a2f2f706f7365722e707567782e6f72672f68706f6c74686f662f6c61726176656c2d6170692f6c6963656e73652e737667)](https://packagist.org/packages/hpolthof/laravel-api)[![Total Downloads](https://camo.githubusercontent.com/525b6fb47588046d5e5e848f877b55f52435aaa58fa5c3385c0aab7134dfb847/68747470733a2f2f706f7365722e707567782e6f72672f68706f6c74686f662f6c61726176656c2d6170692f642f746f74616c2e706e67)](https://packagist.org/packages/hpolthof/laravel-api)

This package was created for an internal project, but as the idea is reusable, I encourage others to make use of this package.

The main goal was to create an uniform way of presenting API output, and the creation of a layer between data structure and output.

Installation
------------

[](#installation)

Require this package with composer:

```
composer require hpolthof/laravel-api

```

Add the follow service provider to your config/app.php:

```
'Hpolthof\LaravelAPI\APIServiceProvider',

```

Middleware
----------

[](#middleware)

A new middleware named `api.errors` will be added to your list of available middleware.

> If you're using Laravel 5.3, this middleware will also be added into the `api` middleware group.

Usage
-----

[](#usage)

To use this package you should implement the `Hpolthof\LaravelAPI\Contracts\ShouldMorphAPI` interface onto an Eloquent model.

You'll have to implement the function `bindAPI()` and have to return an instance of `Hpolthof\LaravelAPI\Binding`.

Like this:

```
public function bindAPI()
{
    return Binding::create([
        'street' => $this->street,
        'street_nr' => $this->number,
        'street_suffix' => $this->suffix,
        'postcode' => $this->zip,
        'city' => $this->city,
    ]);
}

```

In a controller you can then return the following:

```
public function index()
{
    $items = Address::all();
    return \Response::api($items);
}

public function show($id)
{
    $item = Address::find($id);
    return \Response::api($item);
}

```

The response would look something like this:

```
{
    "header": {
        "request": {
            "location": "http:\/\/localhost:8000\/api\/addresses",
            "method": "GET",
            "parameters": []
        },
        "response": {
            "status": 200,
            "error": null,
            "timestamp": "2016-11-28 14:09:35"
        }
    },
    "content": [
        {
            "street": "Van der Polweg",
            "street_nr": 17,
            "street_suffix": "",
            "postcode": "3384HD",
            "city": "Amersfoort",
        },
        {
            "street": "Van der Polweg",
            "street_nr": 15,
            "street_suffix": "",
            "postcode": "3384HD",
            "city": "Amersfoort",
        }
    ]
}
```

Error messages
--------------

[](#error-messages)

Sometimes you'll need to force an error to the user, this can be done by throwing an exception. The package also provides some specific exceptions that should be used where relevant.

```
Hpolthof\LaravelAPI\Exceptions\AccessDeniedException
Hpolthof\LaravelAPI\Exceptions\BadRequestException
Hpolthof\LaravelAPI\Exceptions\NotFoundException
Hpolthof\LaravelAPI\Exceptions\NotImplementedException

```

This would result in something like:

```
{
    "header": {
        "request": {
            "location": "http:\/\/localhost:8000\/api\/addresses",
            "method": "GET",
            "parameters": []
        },
        "response": {
            "status": 403,
            "error": "Forbidden",
            "timestamp": "2016-11-28 15:05:29"
        }
    }
}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Total

5

Last Release

3408d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/de1aeabf860985f978f94da5a6fe0fee1df693abb2ab2453375bdfc013b3f8ac?d=identicon)[hpolthof](/maintainers/hpolthof)

---

Top Contributors

[![hpolthof](https://avatars.githubusercontent.com/u/1415623?v=4)](https://github.com/hpolthof "hpolthof (3 commits)")

---

Tags

jsonapilaravelrest

### Embed Badge

![Health badge](/badges/hpolthof-laravel-api/health.svg)

```
[![Health](https://phpackages.com/badges/hpolthof-laravel-api/health.svg)](https://phpackages.com/packages/hpolthof-laravel-api)
```

###  Alternatives

[chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

698375.6k5](/packages/chrisbjr-api-guard)[givebutter/laravel-keyable

Add API keys to your Laravel models

187144.5k2](/packages/givebutter-laravel-keyable)[guanguans/laravel-api-response

Normalize and standardize Laravel API response data structure. - 规范化和标准化 Laravel API 响应数据结构。

485.6k](/packages/guanguans-laravel-api-response)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
