2017 © Pedro Peláez
 

library php-japi

Simple framework for building HTTP JSON APIs in PHP

image

docnet/php-japi

Simple framework for building HTTP JSON APIs in PHP

  • Wednesday, May 2, 2018
  • by Docnet
  • Repository
  • 3 Watchers
  • 6 Stars
  • 1,116 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 11 Forks
  • 3 Open issues
  • 7 Versions
  • 6 % Grown

The README.md

Build Status Coverage Status, (*1)

PHP JSON API Library

Version 2 of our library for building HTTP JSON APIs in PHP., (*2)

Some major changes in version 2 - Adopt better code practices, allowing for Dependency Injection - Adopt our new "Single Responsibility Controller" approach - Decouple Router from JAPI container - Use PSR logging - Adopt PHP 5.4 minimum version, (*3)

As we expand our Service Orientated Architecture (SOA) at Docnet, we're using this more and more - so I hope it's useful to someone else ;), (*4)

Intended to use HTTP status codes wherever possible for passing success/failure etc. back to the client., (*5)

Single Responsibility Controller

We've adopted a new (for us) take on routing and controller complexity in 2.0. As such, where previously, you might have had multiple actions (methods) on the same class like this:, (*6)

  • BasketController::fetchDetailAction()
  • BasketController::addAction()
  • BasketController::removeAction()
  • BasketController::emptyAction()

Now this would be 4 name-spaced classes, like this, (*7)

  • Basket\FetchDetail
  • Basket\Add
  • Basket\Remove
  • Basket\Empty

This allows for - Greater code modularity - Smaller classes - Much easier Dependency Injection via __construct() as each "action" is it's own class., (*8)

You can still share common code via extension/composition - whatever takes your fancy!, (*9)

JAPI will call the dispatch() method on your controller., (*10)

SOLID Routing

The bundled router will accept any depth of controller namespace, like this, (*11)

  • /one => One
  • /one/two => One\Two
  • /one/two/three => One\Two\Three

When you construct the Router, you can give it a "root" namespace, like this:, (*12)

$router = new \Docnet\JAPI\SolidRouter('\\Docnet\\App\\Controller\\');

Which results in this routing:, (*13)

  • /one/two => \Docnet\App\Controller\One\Two

Static Routes

If you have some static routes you want to set up, that's no problem - they also bypass the routing regex code and so make calls very slightly faster., (*14)

Add a single custom route, (*15)

$router = new \Docnet\JAPI\SolidRouter();
$router->addRoute('/hello', '\\Some\\Controller');

Or set a load of them, (*16)

$router = new \Docnet\JAPI\SolidRouter();
$router->setRoutes([
    '/hello' => '\\Some\\Controller',
    '/world' => '\\Other\\Controller'
]);

Installation

Here's the require line for Composer users (during 2-series development)..., (*17)

"docnet/php-japi": "2.0.*@dev", (*18)

...or just download and use the src folder., (*19)

Bootstrapping

Assuming..., (*20)

  • You've got Apache/whatever set up to route all requests to this file
  • An auto-loader is present (like the Composer example here) or you've included all files necessary

...then something like this is all the code you need in your index.php, (*21)

(new \Docnet\JAPI())->bootstrap(function(){

    $obj_router = new \Docnet\JAPI\SolidRouter();
    $obj_router->route();

    $str_controller = $obj_router->getController();
    return new $str_controller();

});

See the examples folder for a working demo (api.php)., (*22)

Coding Standards

Desired adherence to PSR-2. Uses PSR-3 logging., (*23)

The Versions

02/05 2018

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/DocnetUK/php-japi

Simple framework for building HTTP JSON APIs in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

api json http docnet

02/05 2018

v1.1.2

1.1.2.0 https://github.com/DocnetUK/php-japi

Simple framework for building HTTP JSON APIs in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

api json http docnet

25/08 2016

dev-master

9999999-dev https://github.com/DocnetUK/php-japi

Simple framework for building HTTP JSON APIs in PHP. 2.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Tom Walder

api json http docnet

25/11 2015

dev-error-handling

dev-error-handling https://github.com/DocnetUK/php-japi

Simple framework for building HTTP JSON APIs in PHP. 2.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Tom Walder

api json http docnet

27/10 2015

v2.0.0

2.0.0.0 https://github.com/DocnetUK/php-japi

Simple framework for building HTTP JSON APIs in PHP. 2.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Tom Walder

api json http docnet

01/03 2015

v1.1.1

1.1.1.0 https://github.com/DocnetUK/php-japi

Simple framework for building HTTP JSON APIs in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

api json http docnet

23/06 2014

v1.1.0

1.1.0.0 https://github.com/DocnetUK/php-japi

Simple framework for building HTTP JSON APIs in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

api json http docnet