kigkonsult/restserver

rest services made easy, restServer: PSR http message, builtin ip, cors, auth, serializers and decoders


Keywords
http, rest, response, server, service, request, psr, psr-7, http-message
License
AGPL-1.0

Documentation

restServer - rest services made easy

restServer provides

  • a PSR HTTP message rest server
  • builtin ip, cors, auth, serializing and decoding mgnt
  • API for your application

Install

To install with composer:

composer require kigkonsult/restserver

Requires PHP 5.6, 7.*

Usage

Example usage:

<?php

require '/path/to/vendor/autoload.php';

// Implement an application rest service operation entry
// (or any callable with the same interface)
$callback = function(
    ServerRequestInterface $request,
    ResponseInterface      $response
) {
    return $response->withRawBody( [ 'Hello' => 'world' ] );
};

// Set up the rest service definition (method, uri and callback)
$restGetServiceDef = [
    RestServer::METHOD   => RequestMethodHandler::METHOD_GET,
    RestServer::URI      => '/',
    RestServer::CALLBACK => $callback
];

// Attach the rest service(s) and fire of the server
RestServer::factory()->attachRestService( $restGetServiceDef )->run();

More example usage:

<?php

require '/path/to/vendor/autoload.php';

$RestServer = new RestServer();

$attachRestServiceCallback = $RestServer->getAttachRestServiceCallback();

class ApplicationClass2
{
    public function registerAsRestService(
        $attachRestServiceCallback
    ) {
        $attachRestServiceCallback(
            RequestMethodHandler::METHOD_GET,
            '/',
            [$this, 'action']
        );
    }

    public function action(
        ServerRequestInterface $request,
        ResponseInterface      $response
    ) {
        return $response->withRawBody( ['msg' => 'Hello world'] );
    }
}

$ApplicationClass2 = new ApplicationClass2();
$ApplicationClass2->registerAsRestService( $attachRestServiceCallback );

$RestServer->run();

Rest service definition

You have to implement one or more rest service (callable) entries for your application logic. Each entry with one or more http request methods and a (single) uri (ex '/') form a service definition. The service definitions, attached to restServer, are interfaces to your application logic.

Handlers

restServer have builtin handlers managing IPnumber validation, Cross-Origin Resource Sharing, authentication, messages serializing and en-/decoding. As well as rest service definitions, you can attach custom request message handler(s), invoked before any operation callback. Also a custom (single) final handler can be attached, invoked after response is returned.

Documentation

In the restServer package docs folder are found

  • summary and supplementary documentation
  • demo applications and service definitions
  • demo handlers
  • more examples

For restServer issues, use github. Due to the restServer development status (ver 0.9.123), review reports are appreciated!

Credits and base software information

Built status

Dev 0.9.123