PHPackages                             vinelab/api-manager - 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. vinelab/api-manager

ActiveLibrary[API Development](/categories/api)

vinelab/api-manager
===================

Laravel API Manager Package - beatify and unify your responses with the least effort possible.

v0.7.5(10y ago)382.1k7[2 issues](https://github.com/Vinelab/api-manager/issues)MITPHPPHP &gt;=5.4.0

Since Oct 10Pushed 10y ago9 watchersCompare

[ Source](https://github.com/Vinelab/api-manager)[ Packagist](https://packagist.org/packages/vinelab/api-manager)[ RSS](/packages/vinelab-api-manager/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (7)Versions (23)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/a3ec350bee8e087c2c2b26b61cd9344b1a756b5bc865a96527081665f1706580/68747470733a2f2f706f7365722e707567782e6f72672f76696e656c61622f6170692d6d616e616765722f762f737461626c652e737667)](https://packagist.org/packages/vinelab/api-manager)[![Total Downloads](https://camo.githubusercontent.com/7813db45d8f0e63c2d6bd7c83567d88bd54b5426ae326fea2bb77ec2c21eed67/68747470733a2f2f706f7365722e707567782e6f72672f76696e656c61622f6170692d6d616e616765722f646f776e6c6f6164732e737667)](https://packagist.org/packages/vinelab/api-manager)[![Latest Unstable Version](https://camo.githubusercontent.com/821f2134ca375f0cd130ad86d48f4b7f6e5c70dbf6de14da840970c87cd5a8da/68747470733a2f2f706f7365722e707567782e6f72672f76696e656c61622f6170692d6d616e616765722f762f756e737461626c652e737667)](https://packagist.org/packages/vinelab/api-manager)[![License](https://camo.githubusercontent.com/b694892b0c699c01d0bc5b2d7d7ddc8fc8f7da25cfe5772799e3799a031ad43a/68747470733a2f2f706f7365722e707567782e6f72672f76696e656c61622f6170692d6d616e616765722f6c6963656e73652e737667)](https://packagist.org/packages/vinelab/api-manager)[![Build Status](https://camo.githubusercontent.com/2fb6bc030c939a1eb18e01db89fcbfc0228f326b72e1453f6e363b4ad69df430/68747470733a2f2f7472617669732d63692e6f72672f56696e656c61622f6170692d6d616e616765722e737667)](https://travis-ci.org/Vinelab/api-manager)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1d25fa139ef3061b255ccb1fee90200b4cfa20667ff0c785bd92ec0377d3d385/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f56696e656c61622f6170692d6d616e616765722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Vinelab/api-manager/?branch=master)

API Manager
===========

[](#api-manager)

A simple API response formatter and handler for Laravel. Beautify and unify your responses with the least effort possible.

Install
-------

[](#install)

### Via composer:

[](#via-composer)

```
{
     "require": {
         "vinelab/api-manager": "*"
     }
 }
```

Add the service provider to the `providers` array in `app/config/app.php`:

```
'providers' => array(
    ...
    'Vinelab\Api\ApiServiceProvider',

),
```

Configuration
-------------

[](#configuration)

Publish the package config file:

```
php artisan config:publish vinelab/api-manager
```

It is now located at `app/config/packages/vinelab/api-manager/api.php`

#### Namespace

[](#namespace)

The mappers is where you specify your mappers base namespace (See [Mappers Terminology](#mappers) for more on mappers)

```
'mappers' => 'Lib\Api\Mappers\\',
```

You may use `Api::setMapperNamespace('\New\Namespace\\');` to override the configured namespace.

#### Limit

[](#limit)

The limit is where you set the maximum number of data to be returned with any endpoint request.

```
'limit' => '50',
```

Setup
-----

[](#setup)

This package expects to have **Mapper class** for each model you want to return via the API.

### Mappers

[](#mappers)

A Mapper is a class that transforms any supported data type *(i.e. Model)* into a suitable array for an API response with the attributes of your choice.

By default the *Api Manager* will call the method `map` on the given mapper unless indicated differently by passing `[$mapper, $method]` where `$mapper` is either the actual instance of the class name as a string.

*Example:*

#### Mapper Class

[](#mapper-class)

```
class PostMapper {

    public function map(Post $post)
    {
        return [
            'id'        => (int) $post->id,
            'title'     => $post->title,
            'body'      => $post->body,
            'published' => (boolean) $post->published
        ];
    }
}
```

#### Mapper Usage

[](#mapper-usage)

```
$post = Post::create([
    'title'     => 'Some Title',
    'body'      => 'Things and spaces',
    'published' => true
]);

$mapper = new PostMapper;
return $mapper->map($post);
```

### Mapper Examples

[](#mapper-examples)

Here are some examples implementing a mapper called `PostsMapper`

#### Array Mapping

[](#array-mapping)

```
