2017 © Pedro Peláez
 

library slim-json-api

Slim extension to implement fast JSON API's

image

voku/slim-json-api

Slim extension to implement fast JSON API's

  • Saturday, December 23, 2017
  • by voku
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3,378 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 62 Forks
  • 0 Open issues
  • 15 Versions
  • 0 % Grown

The README.md

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

slim-json-api

WARNING: this is only q maintained Fork of "https://github.com/entomb/slim-json-api", (*2)

This is an extension to the SLIM framework to implement json API's with great ease., (*3)

Installation

Using composer you can add use this as your composer.json, (*4)

{
  "require": {
    "slim/slim": "2.*",
    "voku/slim-json-api": "2.*"
  }
}

Usage

To include the middleware and view you just have to load them using the default Slim way. Read more about Slim Here (https://github.com/codeguy/Slim#getting-started), (*5)

    require 'vendor/autoload.php';

    $app = new \Slim\Slim();

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

.htaccess sample

Here's an .htaccess sample for simple RESTful API's, (*6)

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

example method

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

example code

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

example output

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

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., (*8)

$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., (*9)

$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

It is possible to separate response metadata and business information in separate containers., (*10)

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

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

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., (*11)

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

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., (*12)

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

The Versions

23/12 2017

dev-master

9999999-dev https://github.com/voku/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

MIT

The Requires

 

The Development Requires

api middleware json slim view

23/12 2017

4.0.0

4.0.0.0 https://github.com/voku/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

MIT

The Requires

 

The Development Requires

api middleware json slim view

26/11 2017

3.0.0

3.0.0.0 https://github.com/voku/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

MIT

The Requires

 

The Development Requires

api middleware json slim view

25/10 2016

2.0.1

2.0.1.0 https://github.com/voku/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

MIT

The Requires

 

The Development Requires

api middleware json slim view

25/10 2016

2.0.0

2.0.0.0 https://github.com/voku/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

MIT

The Requires

 

The Development Requires

api middleware json slim view

13/06 2016

1.2.3

1.2.3.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

api middleware json slim view

13/01 2016

1.2.2

1.2.2.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

13/01 2016

1.2.1

1.2.1.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

12/01 2016

1.2.0

1.2.0.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

08/01 2016

1.1.1

1.1.1.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

25/08 2014

1.1.0

1.1.0.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

19/03 2014

1.0.6

1.0.6.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

02/12 2013

1.0.4

1.0.4.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

27/09 2013

1.0.2

1.0.2.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view

17/05 2013

0.1

0.1.0.0 https://github.com/entomb/slim-json-api

Slim extension to implement fast JSON API's

  Sources   Download

GPL 3.0

The Requires

  • php >=5.3.0

 

api middleware json slim view