WellRESTed

WellRESTed is a library for creating RESTful APIs and websites in PHP that provides abstraction for HTTP messages, a powerful middleware system, and a flexible router.

<?php

namespace App;

use DI;
use Psr\Container\ContainerInterface;
use WellRESTed\Server;

require_once 'vendor/autoload.php';

$builder = new DI\ContainerBuilder();
$builder->addDefinitions([
    Server::class => function (ContainerInterface $c): Server {
        $server = new Server();
        $server->add(ErrorMiddleware::class);

        $router = $server->createRouter();
        $server->addRoute($router);

        $router
            ->register('GET', '/', HomeHandler::class)
            ->register('GET', '/cats/', CatsListHandler::class)
            ->register('POST', '/cats/', [
                AuthMiddleware::class,
                JsonMiddleware::class,
                CatCreateHandler::class
            ])
            ->register('GET', '/cats/{id}', CatHandler::class)
            ->register('PUT', '/cats/{id}', [
                AuthMiddleware::class,
                JsonMiddleware::class,
                CatUpdateHandler::class
            ])
            ->register('DELETE', '/cats/{id}', [
                AuthMiddleware::class,
                CatDeleteaHandler::class
            ]);

        return $server;
    }
]);

$container = $builder->build();
$server = $container->get(Server::class);
$server->respond();

Features

PSR-7

Requests and responses are built to the interfaces standardized by PSR-7 making it easy to share code and use components from other libraries and frameworks.

PSR-15

Build your application using handlers and middleware implementing the interfaces defined by PSR-15.

Router

Use a router to map HTTP methods and paths to hanlders, middleware, or sub-routers.

URI Templates

Use URI templates like /foo/{bar}/{baz}, /list/{items*}, or {/path*} that match patterns of paths and provide captured variables.

Lazy Dispatching

WellRESTed's dispatcher can delay instantiating handlers until they're needed, so even large apps can stay light weight.

Dependency Injection

WellRESTed does not supply a DI container, but can work with any an any PSR-11 container.

Installation

The recommended method for installing WellRESTed is to use Composer. Add an entry for WellRESTed in your project’s composer.json file.

{
  "require": {
     "wellrested/wellrested": "^6"
  }
}

Requirements

Documentation

The documentaiton for WellRESTed is available at https://wellrested.readthedocs.io