PHPackages                             gijsbos/apiserver - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. gijsbos/apiserver

ActiveLibrary[API Development](/categories/api)

gijsbos/apiserver
=================

API Server

1.9.28(2mo ago)0253MITPHP

Since Nov 9Pushed 2mo agoCompare

[ Source](https://github.com/Gijsbos/apiserver)[ Packagist](https://packagist.org/packages/gijsbos/apiserver)[ RSS](/packages/gijsbos-apiserver/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (16)Versions (99)Used By (0)

API Server
==========

[](#api-server)

[![PHP Version](https://camo.githubusercontent.com/c5e8da782b1a0673c08b4f474108036d2cc973470eed2d5d89d48e8c8475eee6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e322d626c75652e737667)](https://www.php.net/)[![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](LICENSE)[![Build Status](https://camo.githubusercontent.com/e505712cd2ef0858ff8d570504c8efbf6d983231465d8d6826b20be2c557a68a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f67696a73626f732f6170697365727665722f63692e796d6c3f6272616e63683d6d61696e)](https://github.com/gijsbos/apiserver/actions)[![Issues](https://camo.githubusercontent.com/7bed233ea0d014bb701edea6cd9822cecbe5150022acc5640c47d096f3ca0741/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f67696a73626f732f617069736572766572)](https://github.com/gijsbos/apiserver/issues)[![Last Commit](https://camo.githubusercontent.com/22f15c16c8d9c17c5e1a138f5105509a251738cbca873a5b9d10163e07dba5da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f67696a73626f732f617069736572766572)](https://github.com/gijsbos/apiserver/commits/main)

---

Introduction
------------

[](#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
------------

[](#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
------------

[](#installation)

```
composer require gijsbos/apiserver

```

Setup
-----

[](#setup)

### Controllers

[](#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)

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 to `empty string`.
- `RequestParam|int $id` - Expects an `int` value, cast to `int` or defaults to `empty string`.
- `RequestParam|float $id` - Expects an `float` value, cast to `float` or defaults to `empty string`.
- `RequestParam|double $id` - Expects an `int` value, cast to `double` or defaults to `empty string`.
- `RequestParam|bool $id` - Expects an `int` value, cast to `bool` or defaults to `empty string`.

Allowing `null` values requires you to add the null union type:

- `RequestParam|string|null $id` - Expects a string value, when not defined defaults to `null`.

Route parameter options can be defined by defining the object inside the parameter definition:

```
PathVariable|string $id = new PathVariable(["min" => 0, "max" => 999, "required" => true, "pattern" => "/\d+/", "default" => 1])

```

The following options can be used:

- min *int* - minimum value for `int` values, minimum length for `string` values.
- max *int* - maximum value for `int` values, maximum length for `string` values.
- 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

[](#example)

```
class UserController extends RouteController
{
    #[GetRoute('/user/{id}/')]
    #[ReturnFilter(['name','id'])]
    public function getUser(
        PathVariable|string $id = new PathVariable(["min" => 0, "max" => 999, "required" => false]),
        RequestParam|string $name = new RequestParam(["min" => 0, "max" => 10, "pattern" => "/^[a-z]+$/", "required" => false, "default" => "john"]),
    )
    {
        return [
            "id" => "",
            "name" => $name,
        ];
    }
}

```

### Server

[](#server)

Create a server by defining the following in your index.php. Every request must be rewritten to the index.php file.

```
try
{
    $server = new Server([
        "requireHttps" => false,        // Must use HTTPS or receive error
        "pathPrefix" => "",             // Used for subpaths e.g. localhost/mysubpath/
        "escapeResult" => true,         // Escapes special characters
        "addServerTime" => false,       // Adds code execution time
        "addRequestTime" => false,      // Adds total server response time
    ]);

    $server->listen();
}
catch(RuntimeException | Exception | TypeError | Throwable $ex)
{
    print($ex->getMessage());
}

```

### Caching Routes

[](#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.

```
./bin/api cache routes -(v)erbose (--autoload )

```

Enable APCU in your PHP build for faster route resolving. Build Docker images with the following command:

```
RUN pecl install apcu && docker-php-ext-enable apcu

```

### Contributions

[](#contributions)

Contributions are welcome!
Please open an issue or submit a pull request following our contribution guidelines.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance87

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Recently: every ~16 days

Total

98

Last Release

65d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5a5d143a034f4548fb7df91dd675abb40243d39cc8f8cf306883df50f6fe377b?d=identicon)[Gijsbos](/maintainers/Gijsbos)

---

Top Contributors

[![Gijsbos](https://avatars.githubusercontent.com/u/2572497?v=4)](https://github.com/Gijsbos "Gijsbos (121 commits)")

---

Tags

apiapi-serverPHP-APIapiserver

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gijsbos-apiserver/health.svg)

```
[![Health](https://phpackages.com/badges/gijsbos-apiserver/health.svg)](https://phpackages.com/packages/gijsbos-apiserver)
```

###  Alternatives

[phplicengine/bitly

Bitly API v4

22277.3k](/packages/phplicengine-bitly)[m165437/laravel-blueprint-docs

API Blueprint Renderer for Laravel

22779.0k](/packages/m165437-laravel-blueprint-docs)[hg/apidoc-thinkphp

thinkphp API文档自动生成

1311.8k](/packages/hg-apidoc-thinkphp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
