Giter Club home page Giter Club logo

jsonapi's Introduction

jsonapi

Simple and friendly library for api servers (PHP serving out JSON).

It generates json output according to the jsonapi.org standard, but aims to be easy to understand for people without knowledge of the jsonapi standard.

Getting started

A small example:

use alsvanzelf\jsonapi;

$user = new stdClass();
$user->id = 42;
$user->name = 'Zaphod Beeblebrox';
$user->heads = 2;

$jsonapi = new jsonapi\resource($type='user', $user->id);
$jsonapi->fill_data($user);
$jsonapi->send_response();

Which will result in:

{
    "links": {
        "self": "/examples/resource.php"
    },
    "data": {
        "type": "user",
        "id": 42,
        "attributes": {
            "name": "Zaphod Beeblebrox",
            "heads": 2
        },
        "links": {
            "self": "/examples/resource.php"
        }
    }
}

For a collection response, data is an array of resources. Errors can also be send as response, even automatically by exceptions.

Examples for all kind of responses are in the /examples directory.

Installation

Use Composer. And use require to get the latest stable version:

composer require alsvanzelf/jsonapi

To Do

Right now, this library handles all the basics:

  • generating single resources
  • generating resource collections
  • handling error responses
  • sending out the json response with correct http headers

Plus some handy tools:

  • easy turning thrown exceptions into jsonapi responses
  • constants for easy setting http status codes
  • sending out redirect locations

Plans for the near and later future include:

  • import a database array as a collection response (#2)
  • accept a collection as to-many relation in a resource (#3)
  • handle creating, updating and deleting resources (#5)

Contributing

Pull Requests or issues are welcome!

Licence

MIT

jsonapi's People

Contributors

lode avatar

Watchers

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