PHPackages                             calebdre/api-sugar - 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. calebdre/api-sugar

ActiveLibrary[API Development](/categories/api)

calebdre/api-sugar
==================

A library that makes creating apis easier

3281PHP

Since Jan 5Pushed 10y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Api Sugar
=========

[](#api-sugar)

> A library that allows for quick and easy API creation

This library uses [Flight](http://flightphp.com/) and [Eloquent](http://laravel.com/docs/5.1/eloquent) for routing and database interactions.
You shouldn't need to look at Flight's api except for setup, but the Eloquent documentation is important to look at if you aren't aleady familiar. [Find it here](http://laravel.com/docs/5.1/eloquent)

Usage:
------

[](#usage)

#### Set up server routing

[](#set-up-server-routing)

First you'll need to make sure that all urls reroute to your index.php file.

For *Apache*, edit your `.htaccess` file with the following:

```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

```

For *Nginx*, add the following to your server declaration:

```
server {
    location / {
        try_files $uri $uri/ /index.php;
    }
}

```

### Include the project via composer

[](#include-the-project-via-composer)

run `composer require calebdre/api-sugar`

#### Create your endpoint class

[](#create-your-endpoint-class)

Create a class that contains the endpoints and methods for the api:

```
class UserController extends ApiController{

    public $mappings = [
        'crud' => ['model' => 'Namespace\To\Model', 'resource_name' => 'users'],
	'fetchComment' => ['method' => 'get', 'route' => '/users/comment/@id'
    ];

    public function fetchComment($id){
	// do fetch comment stuff
    }
}

```

Lets break class down:
`class UserController extends ApiController`
All endpoint classes need to extend the ApiController class.

`$mappings`
This is the array that you can add to to create api endpoints. The structure is:
`'methodName' => ['method' => 'get/post/put/delete', 'route' => '/end/point/path']`
The method name is the name of the method in the class that should be called when the route is hit.

You can also pass in `crud` to the array to scaffold CRUD route for an Eloquent model. the syntax for that is:
`'crud' => ['model' => 'namespace/to/model/class', 'resource_name' => 'nameOfRoutePrefix']`

#### Instatiate the API object and configure the database

[](#instatiate-the-api-object-and-configure-the-database)

```
	$api = new Api();
	$api->configureDB([
	    'driver'    => 'driver',
	    'host'      => 'host',
	    'database'  => 'database',
	    'username'  => 'username',
	    'password'  => 'pass',
	    'charset'   => 'utf8',
	    'collation' => 'utf8_unicode_ci',
	    'prefix'    => '',
	]);

	$api->addClass(new UserController());
	$api->execute();

```

You can add the class that you just created with the `addClass` method. To activate the api, use the `execute` method at the end.
You can also use the `addEndPoint($methodType, $routeName, Callable $callable)` function to add routes manually.

After that you should have everything good to go!

### Note on returning json

[](#note-on-returning-json)

In order to return json in your methods, make sure to use `Flight::json($array)` to create a json response. *DO NOT USE RETURN.*

Please feel free to contribute to this project!

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

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

---

Top Contributors

[![calebdre](https://avatars.githubusercontent.com/u/3318929?v=4)](https://github.com/calebdre "calebdre (17 commits)")

### Embed Badge

![Health badge](/badges/calebdre-api-sugar/health.svg)

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

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

265103.1M454](/packages/google-gax)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)

PHPackages © 2026

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