PHPackages                             kingsoft/persist-rest - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. kingsoft/persist-rest

ActiveLibrary[API Development](/categories/api)

kingsoft/persist-rest
=====================

REST interface for \\Kingsoft\\Persist classes

2.13.0(8mo ago)0243[11 issues](https://github.com/theking2/kingsoft-persist-rest/issues)[1 PRs](https://github.com/theking2/kingsoft-persist-rest/pulls)MITPHP

Since Feb 5Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/theking2/kingsoft-persist-rest)[ Packagist](https://packagist.org/packages/kingsoft/persist-rest)[ RSS](/packages/kingsoft-persist-rest/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (10)Dependencies (5)Versions (38)Used By (0)

Kingsoft / Persist REST
=======================

[](#kingsoft--persist-rest)

This package uses \\Kingsoft\\Http, \\Kingsoft\\PersistDb to expose all the tables and views discovered by PersistDb `discover.php` and of those the ones added to the `allowedEndPoints` list. If it is not on that list the api will return a `404`. So other data is save. A table or view is accessible with, GET, POST, PUT, DELETE reqeests but those can also be restricted using `allowedMethods`. The reqeust follow the [rfc9205](https://www.rfc-editor.org/rfc/rfc9205.html) standard with some extensions. This module is `CORS` complient and can allow or disallow certain methods and communicate that fact by properly responding to a `OPTION` request. The resulting service it `HATEOAS` enabled as it provides uris for pagination and other functions.

Methods
-------

[](#methods)

- POST `https://example.com/resource` will create a new record with the values specifed in the json payload. If the record has autoincrment true a new ID is created and returned (C)
- GET `https://example.com/resource` gets a list of all resources restricted by `maxresults` (R)
- GET `https://example.com/resource[n,c]` gets a list of resource starting with position `n` and limited by `c`. The response will include links for pagination. (R)
- GET `https://example.com/resource/` get the record with `id` specified (R)
- GET `https://example.com/resource?=` gets all the recources where attribute `key` = `value`. Combine mutliples with `&` (R)
- GET `https://example.com/resource?=!` gets all the recources where attribute `key` != `value`. (R)
- GET `https://example.com/resource?=U,` gets all the recources where attribute `key` IN (`value1`, `value2`) other operators are avaiable. See \\Kingsoft\\Persist for those (R)
- PUT `https://example.com/resource/` will set new values specifed in the json payload (U)
- DELETE `https://example.com/resource/` deletes the resource with the `id` specified (D)

So the facade creates a complete CRUD interface for all resources (tables, views) exposed. Tables and views can be discoverd using the "discover" feature from `\Kingsoft\Persist`. This will allow to have full access to tables and read access to views or even stored procedures if they produce a result set.

Results
-------

[](#results)

GET results are stored in a `resources` object in the json response allongside links and other messages to make a semi level 3. The results can be a single or multiple objects up to the maximum set. The response will include the urls to implement a pagination.

HATEOAS
-------

[](#hateoas)

Results contain URIs for pagination and other navigation.

Pre flight
----------

[](#pre-flight)

Pre-flight is handled by observing the OPTION method and returning the proper hints where the caching can be specified with the `[api][maxage]`setting:

- Access-Control-Allow-Headers: Content-Type
- Access-Control-Allow-Origen
- Access-Control-Request-Method
- Origin

[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)

Sample usage
------------

[](#sample-usage)

```
use Kingsoft\Http\{StatusCode, Response};
use Kingsoft\PersistRest\{PersistRest, PersistRequest};

try {
  $request = new PersistRequest(
    ['Test', 'TestView'],
    "GET, POST",
    "client.example.com",
  );
  $request->setLogger( LOG );
  $api = new PersistRest( $request, LOG );
  $api->handleRequest();
} catch ( Exception $e ) {
  Response::sendError( $e->getMessage(), StatusCode::InternalServerError->value );
}
```

Discover
--------

[](#discover)

To discover the tables (and views) in you database use this `discover.php` in order to create the class files for access. Make sure the DB connection is made in `config.php` and the `SETTINGS` global array constant is defined. The class `\Kingsoft\Db\Database` needs the following configuration:

```
const SETTINGS = [
    'api' => [
        'namespace' => 'kingsoft\\api'
    ],
    'db'  => [
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'password',
        'database' => 'database'
    ]
];
```

where `namespace` can be adjusted. Discovery can than commence with:

```
