GithubHelp home page GithubHelp logo

codelingoteam / php-japi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from docnetuk/php-japi

0.0 2.0 0.0 74 KB

Simple framework for building HTTP JSON APIs in PHP

License: Apache License 2.0

PHP 100.00%

php-japi's Introduction

Build Status Coverage Status

PHP JSON API Library

Version 2 of our library for building HTTP JSON APIs in PHP.

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

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 ;)

Intended to use HTTP status codes wherever possible for passing success/failure etc. back to the client.

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:

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

Now this would be 4 name-spaced classes, like this

  • 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.

You can still share common code via extension/composition - whatever takes your fancy!

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

SOLID Routing

The bundled router will accept any depth of controller namespace, like this

  • /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:

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

Which results in this routing:

  • /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.

Add a single custom route

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

Or set a load of them

$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)...

"docnet/php-japi": "2.0.*@dev"

...or just download and use the src folder.

Bootstrapping

Assuming...

  • 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

(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).

Coding Standards

Desired adherence to PSR-2. Uses PSR-3 logging.

php-japi's People

Contributors

craig-mcmahon avatar kabudu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.