|
1 | 1 | # Agile API Framework |
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/atk4/api) |
4 | | -[](https://codeclimate.com/github/atk4/api) |
5 | 4 | [](https://styleci.io/repos/107142772) |
6 | 5 | [](https://codecov.io/gh/atk4/api) |
7 | | -[](https://codeclimate.com/github/atk4/api) |
| 6 | +[](https://codeclimate.com/github/atk4/api) |
| 7 | +[](https://codeclimate.com/github/atk4/api/issues) |
8 | 8 |
|
9 | 9 | [](https://packagist.org/packages/atk4/api) |
10 | 10 | [](https://packagist.org/packages/atk4/api) |
11 | 11 |
|
| 12 | +End-to-end implementation for your RESTful API and RPC. Provides a very simple means for you to define API end-points for the application that already uses [Agile Data](https://github.com/atk4/data). |
| 13 | + |
| 14 | +## 1. Simple To Use |
| 15 | + |
| 16 | +Agile API strives to be very simple and work out of the box. Below is a minimal code to get your basic API going, put that into `v1.php` file then invoke `composer require atk4/api` : |
| 17 | + |
| 18 | +``` php |
| 19 | +include 'vendor/autoload.php'; |
| 20 | + |
| 21 | +$api = new \atk4\api\Api(); |
| 22 | + |
| 23 | +// Simple handling of GET request through a callback. |
| 24 | +$api->get('/ping', function() { |
| 25 | + return 'Pong'; |
| 26 | +}); |
| 27 | + |
| 28 | +// Methods can accept arguments, and everything is type-safe. |
| 29 | +$api->get('/hello/:name', function ($name) { |
| 30 | + return "Hello, $name"; |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +## 2. Agile Data Integration |
| 35 | + |
| 36 | +[Agile Data](https://github.com/atk4/data) is a data persistence framework. In simple terms, you can use Agile Data to create your business models (entities) and interact with the database. Agile API is designed to be a perfect integration if you are have already defined classes and persistence in Agile Data. Next code assumes you have `Model Country` and `Persistence $db`: |
| 37 | + |
| 38 | +``` php |
| 39 | +$api->rest('/countries', new Country($db)); |
| 40 | +``` |
| 41 | + |
| 42 | +This creates a standard standard-compliant RESTful interface for interfacing the client model: |
| 43 | + |
| 44 | +- `GET /countries` responds with list of all Country records from $db. |
| 45 | +- `POST /countries` adds a new Country reading data from Form data or JSON in POST body. |
| 46 | +- `GET /countries/123` loads client with specified ID. |
| 47 | +- `PATCH /countries/123` with some Form data or JSON will update existing Country. |
| 48 | +- `DELETE /countries/123` will delete a record. |
| 49 | + |
| 50 | +Through Agile UI you may add conditions, limits and more. Also second argument can be a call-back: |
| 51 | + |
| 52 | +``` php |
| 53 | +$api->rest('/countries', function() use($db) { |
| 54 | + $c = new Country($db)); |
| 55 | + $c->addCondition('is_eu', true); |
| 56 | + $c->setLimit(20); |
| 57 | + return $c; |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +Field types, data conversions, validation and hooks can all be defined through Agile Data, making the API layer very transparent and simple. If you attempt to load non-existant record, API will respond with 404. Other errors will be properly mapped to the API codes or fallback to 500. |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +# Work in progress |
| 66 | + |
| 67 | +Agile UI is still a work in progress. This readme will be further updated to reflect a current features. |
| 68 | + |
12 | 69 |
|
13 | | -End-to-end implementation for your REST API. Provides a very simple means for you to define API end-points for the application that already uses [Agile Data](https://github.com/atk4/data). |
14 | 70 |
|
15 | 71 | ## Planned Features |
16 | 72 |
|
|
0 commit comments