2017 © Pedro Peláez
 

library php-json-server

simple REST API json server

image

zlob/php-json-server

simple REST API json server

  • Thursday, August 13, 2015
  • by Zlob
  • Repository
  • 1 Watchers
  • 9 Stars
  • 64 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 7 Versions
  • 7 % Grown

The README.md

PHP JSON Server

Easy to use library, that provides you REST API server in a few minutes. Created for front-end developers who need a quick back-end for prototyping. May be used with you lovely php web framework, (*1)

Inspired by JSON Server, (*2)

NOT FOR PRODUCTION

Latest Stable Version Total Downloads License Build Status, (*3)

Install

via composer: composer require zlob/php-json-server, (*4)

Example

You can use this library with any php web framework. Here is example how to integrate it with Laravel 5.1:, (*5)

  • First, you need to create controller, where we will use php-json-server:

``` php <?php, (*6)

namespace App\Http\Controllers;, (*7)

use Request; use Response; use Config; use JsonServer\JsonServer;, (*8)

class JsonServerController extends Controller { public function handleRequest($uri) { $data = Request::all(); //request data $method = Request::method(); //request method $jsonServer = new JsonServer(); //create new JsonServer instance $response = $jsonServer->handleRequest($method, $uri, $data); //handle request //return Symfony\Component\HttpFoundation\Response //object with content, status and headers $response->send(); //send response } }, (*9)

* Then, add to routes.php file new route, to link '/api/*' route with our controller method handleRequest
``` php
Route::any('api/{all}', "JsonServerController@handleRequest")->where('all', '.*');
  • Finaly, we can optionaly fill our database with some data. To do this, open php-json-server/db/db.json file and add some data: ``` json { "posts": [ { "id": 1, "title": "json-server", "author": "zlob" }, { "id": 2, "title": "json-server", "author": "zlob" } ], "comments": [ { "id": 1, "body": "some comment", "post_id": 1 } ] }
* Thats all! Now, if you go to "/api/posts" you'll get
{ "id": 1, "title": "json-server", "author": "zlob" }

Based on the previous db.json file, here are all routes:

GET /posts GET /posts/1 GET /posts/1/comments POST /posts PUT /posts/1 PATCH /posts/1 DELETE /posts/1, (*10)


To filter resources

GET /posts?title=json-server&author=zlob, (*11)


To slice resources, add `_start` and `_end` or `_limit`(an X-Total-Count header is included in the response).

GET /posts?_start=20&_end=30 GET /posts/1/comments?_start=0&_end=30, (*12)


To sort resources, add `_sort` and `_order` (ascending order by default).

GET /posts?_sort=views&_order=desc GET /posts/1/comments?_sort=votes&_order=asc, (*13)


To make a full-text search on resources, add `_query`.

GET /posts?_query=internet, (*14)


To embed other resources, add `_embed`(separate by ',' for more then one resources).

GET /posts/1?_embed=comments GET /posts/1?_embed=comments,tags, (*15)


## CLI: Generate random data You can fill database with fake data in one command. In example, to create table posts, with 50 resources, that contains fields title, date, author and content, in jsonServer directory use command:

php-json-server faker posts 'title.text date.date author.name content.text.10' --num=50 ``` Where where fields separated by space, and field name, field type and additional parameter separated by '.' Field type and parameters described in faker, that used inside., (*16)

License

MIT - Zlob, (*17)

The Versions

13/08 2015
13/08 2015

dev-dev-master

dev-dev-master

simple REST API json server

  Sources   Download

MIT

The Requires

 

The Development Requires

json rest api server restful api

13/08 2015
27/07 2015

v1.0.2

1.0.2.0

simple REST API json server

  Sources   Download

MIT

The Requires

 

The Development Requires

json rest api server restful api

22/07 2015

v1.0.1

1.0.1.0

simple REST API json server

  Sources   Download

MIT

The Requires

 

The Development Requires

json rest api server restful api

20/07 2015

v1.0

1.0.0.0

simple REST API json server

  Sources   Download

MIT

The Requires

 

The Development Requires

json rest api server restful api