PHPackages                             voku/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. voku/slim-json-api

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

voku/slim-json-api
==================

Slim extension to implement fast JSON API's

4.0.0(8y ago)13.4k1[1 PRs](https://github.com/voku/slim-json-api/pulls)MITPHPPHP &gt;=7.0.0

Since May 17Pushed 3y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (16)Used By (0)

[![Build Status](https://camo.githubusercontent.com/57666918814f2771cdbcfd4cd87431beb2f3abba38c7bcd658e14d96a8473868/68747470733a2f2f7472617669732d63692e6f72672f766f6b752f736c696d2d6a736f6e2d6170692e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/voku/slim-json-api)[![Coverage Status](https://camo.githubusercontent.com/eb6e998b25897d3a13e6dc214ef10e6ed5fcc991606e02ef7744dba470ed91b2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f766f6b752f736c696d2d6a736f6e2d6170692f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/voku/slim-json-api?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e4dd5d1b635a3e2f835c115b85f58a40feb5e14c9a696f3a34771b6e3115703d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f766f6b752f736c696d2d6a736f6e2d6170692f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/voku/slim-json-api/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/25ee555f1ef372b47bf53b374c06e0127d595499a1a2bd01db6fee641f5fefb1/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f736c696d2d6a736f6e2d6170692f762f737461626c65)](https://packagist.org/packages/voku/slim-json-api)[![Total Downloads](https://camo.githubusercontent.com/36531df1cd52b186658bf99e70c0953163064e643b753057727f4e3902795d33/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f736c696d2d6a736f6e2d6170692f646f776e6c6f616473)](https://packagist.org/packages/voku/slim-json-api)[![Latest Unstable Version](https://camo.githubusercontent.com/3de492d28bd2ad304a5cf924c783a161eb569cb04edab71cdb27a4d858093585/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f736c696d2d6a736f6e2d6170692f762f756e737461626c65)](https://packagist.org/packages/voku/slim-json-api)[![License](https://camo.githubusercontent.com/4a0cb2ce74e93a0329d3286f06d8d131c936022cfb4054f193473a5aa9fda361/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f736c696d2d6a736f6e2d6170692f6c6963656e7365)](https://packagist.org/packages/voku/slim-json-api)

slim-json-api
=============

[](#slim-json-api)

WARNING: this is only q maintained Fork of ""

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.*",
    "voku/slim-json-api": "2.*"
  }
}
```

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 \voku\slim\JsonApiView());
    $app->add(new \voku\slim\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 \voku\slim\JsonApiView("data", "meta"));
$app->add(new \voku\slim\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 \voku\slim\JsonApiView());
  $app->add(new \voku\slim\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

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 57.7% 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 ~129 days

Recently: every ~139 days

Total

14

Last Release

3111d ago

Major Versions

0.1 → 1.0.22013-09-26

1.2.3 → 2.0.02016-10-25

2.0.1 → 3.0.02017-11-26

3.0.0 → 4.0.02017-12-23

PHP version history (2 changes)0.1PHP &gt;=5.3.0

3.0.0PHP &gt;=7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6456fe693db197c458272cb758bf78958bc7d3e787ccd59db4bf3cf41654316a?d=identicon)[voku](/maintainers/voku)

---

Top Contributors

[![entomb](https://avatars.githubusercontent.com/u/57768?v=4)](https://github.com/entomb "entomb (45 commits)")[![voku](https://avatars.githubusercontent.com/u/264695?v=4)](https://github.com/voku "voku (10 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)")[![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)")[![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)")[![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)")

---

Tags

jsonmiddlewareapislimview

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[entomb/slim-json-api

Slim extension to implement fast JSON API's

267157.7k4](/packages/entomb-slim-json-api)[dogancelik/slim-json

JSON middleware for Slim PHP framework

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

InfyOm Laravel Generator GUI Builder

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

Shoot aims to make providing data to your templates more manageable

40229.9k2](/packages/shoot-shoot)[petebrowne/slim-layout-view

A Custom View supporting Layouts for the Slim Framework.

3518.0k](/packages/petebrowne-slim-layout-view)

PHPackages © 2026

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