Download the PHP package gijsbos/apiserver without Composer
On this page you can find all versions of the php package gijsbos/apiserver. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gijsbos/apiserver
More information about gijsbos/apiserver
Files in gijsbos/apiserver
Informations about the package apiserver
API Server
Introduction
Lightweight, attribute-driven PHP API Server that simplifies route definition, request validation, and response filtering. It aims to make building structured REST APIs clean, fast, and type-safe, leveraging PHP 8+ attributes.
Requirements
Before running the application, make sure you have the following installed:
- Web Server: Apache or Nginx
- PHP:
>= 8.2 - Composer: for dependency management
Installation
Setup
Controllers
Extend your controllers with the RouteController class and create a new route by defining attributes:
- Route(
method,path) or short GetRoute, PostRoute, PutRoute, DeleteRoute, PatchRoute, OptionsRoute. - ReturnFilter(
array) - assoc array containing keys to filter out. - RequiresAuthorization() - extends the ExecuteBeforeRoute, provides simple token checking and extraction.
Route Parameters
Route parameters allow you to inject parameters into your controller method.
PathVariable- Extracts parameters from path that have been defined using curly brackets.RequestParam- Extracts parameters from global variables.RequestHeader- Extracts headers from server headers.
Parameter behaviour can be controlled by both defining union types and optional parameters.
RequestParam|string $id- Expects a string value, when not defined defaults toempty string.RequestParam|int $id- Expects anintvalue, cast tointor defaults toempty string.RequestParam|float $id- Expects anfloatvalue, cast tofloator defaults toempty string.RequestParam|double $id- Expects anintvalue, cast todoubleor defaults toempty string.RequestParam|bool $id- Expects anintvalue, cast toboolor defaults toempty string.
Allowing null values requires you to add the null union type:
RequestParam|string|null $id- Expects a string value, when not defined defaults tonull.
Route parameter options can be defined by defining the object inside the parameter definition:
The following options can be used:
- min int - minimum value for
intvalues, minimum length forstringvalues. - max int - maximum value for
intvalues, maximum length forstringvalues. - required bool - when true, throws missing error when parameter is not defined (has no effect on PathVariable).
- pattern string - regexp pattern uses to check the value.
- default mixed - fallback value when value is empty.
- values array - permitted values or throws error.
Example
Server
Create a server by defining the following in your index.php. Every request must be rewritten to the index.php file.
Caching Routes
Routes paths are cached in the cache folder, you can use the bin/api binary to cache routes. When there is no routes file, a cache will be created upon first use.
Enable APCU in your PHP build for faster route resolving. Build Docker images with the following command:
Contributions
Contributions are welcome!
Please open an issue or submit a pull request following our contribution guidelines.
All versions of apiserver with dependencies
gijsbos/stringfuncs Version ^1.2
gijsbos/php-cli-parser Version ^1.0
gijsbos/http Version ^1.2
gijsbos/logging Version ^1.3
gijsbos/extfuncs Version ^1.5
gijsbos/classparser Version ^2.1