PHPackages                             entomb/slim-json-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. [Templating &amp; Views](/categories/templating)
4. /
5. entomb/slim-json-api

ActiveLibrary[Templating &amp; Views](/categories/templating)

entomb/slim-json-api
====================

Slim extension to implement fast JSON API's

1.2.3(9y ago)268156.4k↓20.5%49[10 issues](https://github.com/entomb/slim-json-api/issues)[1 PRs](https://github.com/entomb/slim-json-api/pulls)4MITPHPPHP &gt;=5.3.0

Since May 17Pushed 9y ago24 watchersCompare

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

READMEChangelog (8)DependenciesVersions (11)Used By (4)

slim-jsonAPI
============

[](#slim-jsonapi)

[![Latest Stable Version](https://camo.githubusercontent.com/ec609809192c9786634bef81acc26e4ad3ca8807c348e382cadf4ed64eb0a02a/68747470733a2f2f706f7365722e707567782e6f72672f656e746f6d622f736c696d2d6a736f6e2d6170692f762f737461626c652e706e67)](https://packagist.org/packages/entomb/slim-json-api)[![Total Downloads](https://camo.githubusercontent.com/9cdf41b809d8c4a50d2126daa89d881cca7ec3da46a050fabc0d7d37a34b46f2/68747470733a2f2f706f7365722e707567782e6f72672f656e746f6d622f736c696d2d6a736f6e2d6170692f646f776e6c6f6164732e706e67)](https://packagist.org/packages/entomb/slim-json-api)

This is an extension to the [SLIM framework](https://github.com/codeguy/Slim) to implement json API's with great ease.

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

[](#installation)

Using composer you can add use this as your composer.json

```
    {
        "require": {
            "slim/slim": "2.3.*",
            "entomb/slim-json-api": "dev-master"
        }
    }
```

Usage
-----

[](#usage)

To include the middleware and view you just have to load them using the default *Slim* way. Read more about Slim Here ()

```
    require 'vendor/autoload.php';

    $app = new \Slim\Slim();

    $app->view(new \JsonApiView());
    $app->add(new \JsonApiMiddleware());
```

### .htaccess sample

[](#htaccess-sample)

Here's an .htaccess sample for simple RESTful API's

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

```

### example method

[](#example-method)

all your requests will be returning a JSON output. the usage will be `$app->render( (int)$HTTP_CODE, (array)$DATA);`

#### example Code

[](#example-code)

```
    $app->get('/', function() use ($app) {
        $app->render(200,array(
                'msg' => 'welcome to my API!',
            ));
    });
```

#### example output

[](#example-output)

```
{
    "msg":"welcome to my API!",
    "error":false,
    "status":200
}
```

Errors
------

[](#errors)

To display an error just set the `error => true` in your data array. All requests will have an `error` param that defaults to false.

```
    $app->get('/user/:id', function($id) use ($app) {

        //your code here

        $app->render(404,array(
                'error' => TRUE,
                'msg'   => 'user not found',
            ));
    });
```

```
{
    "msg":"user not found",
    "error":true,
    "status":404
}
```

You can optionally throw exceptions, the middleware will catch all exceptions and display error messages.

```
    $app->get('/user/:id', function($id) use ($app) {

        //your code here

        if(...){
            throw new Exception("Something wrong with your request!");
        }
    });
```

```
{
    "error": true,
    "msg": "ERROR: Something wrong with your request!",
    "status": 500
}
```

Embedding response data and metadata in separate containers
-----------------------------------------------------------

[](#embedding-response-data-and-metadata-in-separate-containers)

It is possible to separate response metadata and business information in separate containers.

#### To make it possible just init JsonApiView with containers names

[](#to-make-it-possible-just-init-jsonapiview-with-containers-names)

```
   require 'vendor/autoload.php';

    $app = new \Slim\Slim();

    $app->view(new \JsonApiView("data", "meta"));
    $app->add(new \JsonApiMiddleware());
```

#### Response

[](#response)

```
{
    "data":{
        "msg":"welcome to my API!"
    },
    "meta":{
        "error":false,
        "status":200
    }
}
```

routing specific requests to the API
------------------------------------

[](#routing-specific-requests-to-the-api)

If your site is using regular HTML responses and you just want to expose an API point on a specific route, you can use Slim router middlewares to define this.

```
    function APIrequest(){
        $app = \Slim\Slim::getInstance();
        $app->view(new \JsonApiView());
        $app->add(new \JsonApiMiddleware());
    }

    $app->get('/home',function() use($app){
        //regular html response
        $app->render("template.tpl");
    });

    $app->get('/api','APIrequest',function() use($app){
        //this request will have full json responses

        $app->render(200,array(
                'msg' => 'welcome to my API!',
            ));
    });
```

middleware
----------

[](#middleware)

The middleware will set some static routes for default requests. **if you dont want to use it**, you can copy its content code into your bootstrap file.

***IMPORTANT: remember to use `$app->config('debug', false);` or errors will still be printed in HTML***

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community32

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 66.2% 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 ~124 days

Recently: every ~39 days

Total

10

Last Release

3626d ago

Major Versions

0.1 → 1.0.22013-09-26

### Community

Maintainers

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

---

Top Contributors

[![entomb](https://avatars.githubusercontent.com/u/57768?v=4)](https://github.com/entomb "entomb (47 commits)")[![mikebarlow](https://avatars.githubusercontent.com/u/293049?v=4)](https://github.com/mikebarlow "mikebarlow (6 commits)")[![clicman](https://avatars.githubusercontent.com/u/1079535?v=4)](https://github.com/clicman "clicman (4 commits)")[![martemorfosis](https://avatars.githubusercontent.com/u/2488715?v=4)](https://github.com/martemorfosis "martemorfosis (4 commits)")[![bignall](https://avatars.githubusercontent.com/u/1179454?v=4)](https://github.com/bignall "bignall (3 commits)")[![woolfg](https://avatars.githubusercontent.com/u/1328269?v=4)](https://github.com/woolfg "woolfg (1 commits)")[![baileylo](https://avatars.githubusercontent.com/u/145345?v=4)](https://github.com/baileylo "baileylo (1 commits)")[![Zyles](https://avatars.githubusercontent.com/u/1741593?v=4)](https://github.com/Zyles "Zyles (1 commits)")[![beamcode](https://avatars.githubusercontent.com/u/73224590?v=4)](https://github.com/beamcode "beamcode (1 commits)")[![bryant1410](https://avatars.githubusercontent.com/u/3905501?v=4)](https://github.com/bryant1410 "bryant1410 (1 commits)")[![Philzen](https://avatars.githubusercontent.com/u/1634615?v=4)](https://github.com/Philzen "Philzen (1 commits)")[![ulion](https://avatars.githubusercontent.com/u/175133?v=4)](https://github.com/ulion "ulion (1 commits)")

---

Tags

jsonmiddlewareapislimview

### Embed Badge

![Health badge](/badges/entomb-slim-json-api/health.svg)

```
[![Health](https://phpackages.com/badges/entomb-slim-json-api/health.svg)](https://phpackages.com/packages/entomb-slim-json-api)
```

###  Alternatives

[dogancelik/slim-json

JSON middleware for Slim PHP framework

3984.0k3](/packages/dogancelik-slim-json)[infyomlabs/generator-builder

InfyOm Laravel Generator GUI Builder

132435.7k](/packages/infyomlabs-generator-builder)[shoot/shoot

Shoot aims to make providing data to your templates more manageable

40229.9k2](/packages/shoot-shoot)

PHPackages © 2026

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