Download the PHP package js361014/plankton without Composer
On this page you can find all versions of the php package js361014/plankton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package plankton
Plankton: a RESTful API microframework
Requirements
- PHP >= 7.2
- PHP cURL extension
- Apache HTTP Server >= 2.4
- Apache mod_rewrite enabled
Installation
composer require foxdie/rest
Table of content
- Client
- Creating a client
- GET example
- using callback
- using magic
- POST example
- using callback
- using magic
- PUT, PATCH and DELETE examples
- Content types
- Custom request example
- Magic calls
- Spinal case
- Examples
- Authentication strategy
- anonymous auth
- basic auth
- client credentials
- Server
- Creating a server
- Handling requests
- Using a config file
- Example of config file
- Configure the server
- Using annotations
- @Route annotation
- @Method annotation
- @Exception annotation
- Registering controllers
- Creating middlewares
- Registering the middlewares
- OAuth2
- Client Credentials Grant
- Client
- Server
- Creating your own Access Token Provider
- Logging
- Client side
- Simple logger
- XML logger
- Custom logger
- Server side
Client
Creating a client
Full example here: https://github.com/foxdie/rest/blob/master/Test/public/simple-client.php
GET example
using callback
using magic
POST example
using callback
using magic
PUT, PATCH and DELETE examples
Full example here: https://github.com/foxdie/rest/blob/master/Test/public/simple-client.php
Magic calls
Spinal case
If you want to use magic calls, your routes must use the spinal case Example:
$client->getUserAccounts()
will match the following route:
GET /user-accounts
camel case and snake case are not supported
Examples
call | route |
---|---|
$client->getUsers(); | GET /users |
$client->groups(1)->getUsers(); | GET /groups/1/users |
$client->groups(1)->getUsers(2); | GET /groups/1/users/2 |
$client->postUsers([]); | POST /users |
$client->groups(1)->postUsers([]); | POST /groups/1/users |
$client->deleteUsers(1); | DELETE /users/1 |
$client->users(1)->delete(); | DELETE /users/1 |
$client->groups(1)->deleteUsers(2); | DELETE /groups/1/users/2 |
$client->groups(1)->users(2)->delete(); | DELETE /groups/1/users/2 |
$client->groups(1)->users()->delete(2); | DELETE /groups/1/users/2 |
Content types
When you are using magic calls (e.g. $client->postUsers([]);
) or one of the methods Client::post()
, Client::put()
, Client::patch()
, a Content-Type
header is automatically added to the request. The Content-Type
is automatically guessed according to the data you send to the server :
data type | Content-Type |
---|---|
array | application/x-www-form-urlencoded |
object | application/json |
valid json string | application/json |
valid xml string | application/xml |
string | text/plain |
However, you still can set the Content-Type
manually with a customized request
Custom request example
Automatic data conversion
For readability reasons, you can use arrays
or objects
with the Request::setData()
method, regardless of the content-type you use. The data will be automatically converted according to the rules below :
Content-Type | Data type | Conversion |
---|---|---|
Request::CONTENT_TYPE_JSON | array | json string |
Request::CONTENT_TYPE_JSON | object | json string |
other | array | URL-encoded query string |
other | object | URL-encoded query string |
Authentication strategy
anonymous auth
basic auth
client credentials
The authorize and access/refresh token requests will be performed automatically. The 3rd parameter is optionnal, the default value is "/token"
Server
Creating a server
Full example here: https://github.com/foxdie/rest/blob/master/Test/public/simple-server.php
Creating controllers
You must create at least one controller which extends the abstract class Plankton\Server\Controller
Your controller will contain one public method for each action of your API.
You can create routes in 2 different ways:
- using a config file
- using annotations
Using a config file
This will automatically disable the annotation parser. The routes are described in a YAML file
Example of config file
Full example here: https://github.com/foxdie/plankton/blob/master/Test/config/server.yml
Configuring the server
Full example here: https://github.com/foxdie/plankton/blob/master/Test/public/config-server.php
Using annotations
The routes will be created automatically according to the annotations @Route and @Method.
Full example here : https://github.com/foxdie/rest/blob/master/Test/Controller/APIController.php
@Route annotation
- accepts regular expresssions
- accepts placeholders: they will be passed as argument in the same order as they appear
- the spinal case is strongly recommended
You can add a route prefix to your controller:
@Method annotation
Possible values are:
- GET
- POST
- PUT
- PATCH
- DELETE
@Exception annotation
Registering controllers
Full example here: https://github.com/foxdie/rest/blob/master/Test/public/simple-server.php
Creating middlewares
(this is optionnal) You must implement the Plankton\Server\Middleware interface. The middlewares can handle both incoming requests and outgoing responses.
Full example here: https://github.com/foxdie/rest/blob/master/Test/Middleware/BasicAuthenticationMiddleware.php
Registering the middlewares
OAuth2
Client Credentials Grant
Client
Full example here:
https://github.com/foxdie/rest/blob/master/Test/public/oauth2-client.php
Server
Full example here: https://github.com/foxdie/rest/blob/master/Test/public/oauth2-server.php
Creating your own Access Token Provider
All you have to do is to implement the AccessTokenProvider interface:
Logging
Client side
Simple logger
Full example here: https://github.com/foxdie/rest/blob/master/Test/public/simple-client.php
XML logger
Full example here: https://github.com/foxdie/rest/blob/master/Test/public/oauth2-client.php
Custom logger
You have to implement the Plankton\Request\Logger interface:
Server side
You can easily log requests and responses by adding a middleware:
and then register the middleware(#registering-the-middlewares)