RestFn
RestFn ("REST function") is a library for building a single, functional-style endpoint.
Instead of many URLs, the client sends one request to one endpoint. The request is
a tree of operations. The server validates that tree, runs it, and returns the
result. You write your own logic as actions, and the client composes those
actions with built-in operations like get, map, sort, ifElse and
sequence to get back the data it needs in a single request.
RestFn gives you the engine for this: an operation language, a request runner, authentication, and a dependency injection container that wires it together. You provide the actions, which are your own code, and you decide which operations the client is allowed to use.
Requirements
This library requires PHP 8.4+ with the json and mbstring extensions loaded.
Installation
Require it in your project with Composer:
composer require arekx/restfn
A first look
Your whole API is one PHP file:
use ArekX\RestFn\App\WebApp;
echo WebApp::createDefault([
'config' => [
'global' => [
// Actions the run operation can call. All operations are available by default.
'actions' => ['getUser' => App\Actions\GetUserAction::class],
],
],
])->run();
A client then sends an operation tree as the JSON body:
["get", "email", ["run", "getUser", 1]]
run calls your getUser action with 1, get pulls out the email field, and
the response is the email. Read Getting Started for the full
walkthrough.
Documentation
- Getting Started - build your first endpoint.
- Architecture - how the pieces fit together.
- Operations - the operation language.
- Cookbook - recipes that compose operations for common tasks.
- Actions - defining your own actions.
- Authentication - protecting actions with tokens.
- Runner and Middleware - the request lifecycle.
- Error Handling - how errors become JSON responses.
- Dependency Injection - how wiring works.
- Configuration - every config value and its default.