PHPackages                             sinclairt/api-foundation - 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. [API Development](/categories/api)
4. /
5. sinclairt/api-foundation

ActiveLibrary[API Development](/categories/api)

sinclairt/api-foundation
========================

A basis for your REST API

2.0.2(8y ago)0661MITPHP

Since Jul 21Pushed 8y agoCompare

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

READMEChangelogDependencies (7)Versions (14)Used By (1)

API Foundation
==============

[](#api-foundation)

A simple trait to build RESTful controllers using Fractal.

### Installation

[](#installation)

Register the service provider in `config/app.php` providers array: ` Sinclair\ApiFoundation\Providers\ApiFoundationServiceProvider::class`

### Usage

[](#usage)

Use the `\Sinclair\ApiFoundation\Traits\ApiFoundation` trait inside your API controllers to create RESTful controllers.

This is written as a trait to prevent inheritance issues, so feel free to overwrite any of the methods.

ApiFoundation ships with a default transformer, which turns the Eloquent model to an array, and with an overload `__call` method to handle includes for additional relationships.

You will need to `__construct` the controller to inject a resource implementation of `Sinclair\Repository\Contracts\Repository` and the transformer you would like to use. You can optionally set the resource name for Fractal to use.

There are fluent setters for the transformer, resource name, and repository should you need them.

##### Available Methods

[](#available-methods)

- index *GET* *paginated collection*
- filter *POST* *paginated collection*
- store *POST* *item*
- show *GET* *item*
- update *PUT/PATCH* *item*
- destroy *DELETE* *item*
- restore *GET* *item*

##### Using Laravel/Eloquent to take the load off!

[](#using-laraveleloquent-to-take-the-load-off)

It is recommended to use the properties that Eloquent provides to reduce the code in the Transformers.

- Use the `$hidden`/`$visible` properties to control what is seen inside an array.
- Use the `$casts` property to control the type the fields are cast to for arrays. See [here](https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting) for more detail.
- Use the `$with` property to control which relations are eager loaded by default.

```
class User extends Model
{
    protected $fillable = ['name', 'email', 'password', 'api_token', 'is_admin'];

    protected $hidden = ['password'];
    // or
    protected $visible = ['name', 'email', 'api_token', 'is_admin'];

    protected $casts = [
        'name'      => 'string',
        'posts'     => 'collection',
        'is_admin'  => 'boolean'
    ];

    protected $with = [
        'posts'
    ];

    public function posts()
    {
        return $this->hasMany(App\Posts::class);
    }
}

```

It would be sensible to add the token auth driver to the api middleware group in `App\Http\Kernel.php`:

```
'api' => [
            'throttle:60,1',
            'auth:token'
        ],

```

But you are free to use your own authorisation driver.

I strongly recommend setting up an API route group such as:

```
Route::group(['middleware' => 'api', 'namespace' => 'Api'], function()
{
    Route::get('api/v1/user/{user}/restore', [
        'as'   => 'api.v1.user.restore',
        'uses' => 'UserController@restore'
    ]);

    Route::post('api/v1/user/{user}/filter', [
        'as'   => 'api.v1.user.filter',
        'uses' => 'UserController@filter'
    ]);

    Route::resource('/api/v1/user', 'UserController', ['except' => ['create', 'edit']]);
}):

```

Use form requests for each resource so you're validation is abstracted away from the controller as well. I'd expect you to over write the `store` and `update` methods, so you can inject your form requests.

Finally, let's use Route Model Binding, here's a quick script I use for this in `App\Providers\RouteServiceProvider`:

```
protected $bindings = [
    'user'
];

public function boot( Router $router )
{
    foreach ( $this->bindings as $model )
    {
        $router->bind(strtolower($model), function ( $value ) use ( $model )
        {
            $model = app(studly_case($model));

            $model = in_array(SoftDeletes::class, class_uses($model)) ? $model->withTrashed()
                                                                              ->find($value) : $model->find($value);

            // the abort is optional
            if ( is_null($model) )
                abort(403);

            return $model;
        });
    }

    parent::boot($router);
}

```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~34 days

Recently: every ~58 days

Total

13

Last Release

3171d ago

Major Versions

0.0.1 → 1.0.02016-09-06

1.2.4 → 2.0.02017-09-07

### Community

Maintainers

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

---

Top Contributors

[![sinclairt](https://avatars.githubusercontent.com/u/16877448?v=4)](https://github.com/sinclairt "sinclairt (9 commits)")[![tom-sinclair](https://avatars.githubusercontent.com/u/23167798?v=4)](https://github.com/tom-sinclair "tom-sinclair (9 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sinclairt-api-foundation/health.svg)

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

###  Alternatives

[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[apiato/core

Core package for Apiato.

70335.0k5](/packages/apiato-core)

PHPackages © 2026

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