PHPackages                             xp-forge/rest-api - 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. xp-forge/rest-api

ActiveLibrary[API Development](/categories/api)

xp-forge/rest-api
=================

Rest API

v5.1.0(10mo ago)241.3k↓11.5%1[3 issues](https://github.com/xp-forge/rest-api/issues)BSD-3-ClausePHPPHP &gt;=7.4.0CI passing

Since Feb 12Pushed 10mo ago3 watchersCompare

[ Source](https://github.com/xp-forge/rest-api)[ Packagist](https://packagist.org/packages/xp-forge/rest-api)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-rest-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (36)Used By (0)

Rest APIs
=========

[](#rest-apis)

[![Build status on GitHub](https://github.com/xp-forge/rest-api/workflows/Tests/badge.svg)](https://github.com/xp-forge/rest-api/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/6e42f1c5db4ecfb96614aebad1aadb663e4dc15b0d684299aeffc3cd3c6ab589/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f726573742d6170692f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/rest-api)

Annotation-based REST APIs

Example
-------

[](#example)

```
use web\rest\{Get, Post, Resource, Response};

#[Resource('/users')]
class Users {

  #[Get('/')]
  public function listUsers($max= 10) {
    // $max comes from request parameter "max", defaulting to 10
    // ...
  }

  #[Get('/{id}')]
  public function findUser($id) {
    // $id is extracted from URL segment
    // ...
  }

  #[Post('/')]
  public function createUser($user) {
    // $user is deserialized from the request body according to its content type
    // ...
    return Response::created('/users/{id}', $id)->entity($created);
  }
}
```

Wire it together in a web application:

```
use web\Application;

class Service extends Application {

  /** @return [:var] */
  public function routes() {
    return ['/users' => new RestApi(new Users())];
  }
}
```

Run it using:

```
$ xp -supervise web Service
@xp.web.Serve(HTTP @ peer.ServerSocket(resource(type= Socket, id= 88) -> tcp://127.0.0.1:8080))
# ...
```

Then call `curl -i localhost:8080/users/1549`.

Parameter sources
-----------------

[](#parameter-sources)

Method parameters are automatically extracted from URI segments if their name matches the path segment in the curly braces. For requests without bodies (GET, HEAD, DELETE, OPTIONS), the value is extracted from request parameters. For requests with bodies (POST, PUT and PATCH), the body is deserialized and passed.

To supply the source explicitely, you can use parameter attributes:

- `#[Param]` will fetch the parameter from the request parameter named "max".
- `#[Param('maximum')]` will fetch the parameter from the request parameter named "maximum".
- `#[Value]` will use a request value (which was previously passed e.g. inside a filter via `pass()`) for the parameter
- `#[Header('Content-Type')]` will use the *Content-Type* header as value for the parameter
- `#[Entity]` will deserialize the request body and pass its value to the parameter
- `#[Body]` will pass the request body as a string
- `#[Stream]` will pass an `io.streams.InputStream` instance to stream the request body to the parameter
- `#[Request]` will pass the complete `web.Request` object

Parameter conversions
---------------------

[](#parameter-conversions)

Parameters can be converted from their input. This library comes with a built-in conversion named *SeparatedBy*:

```
use web\rest\{Resource, Get, Param, SeparatedBy};

#[Resource('/api/trainings')]
class Trainings {

  #[Get('/completed')]
  public function completed(#[Param, SeparatedBy(',')] array $orgunits) {
    return $this->repository->find(['status' => 'COMPLETED', 'orgunits' => $orgunits]);
  }
}
```

The *orgunits* parameter can now be supplied in the URL as follows: `https://example.com/api/trainings/completed?orgunits=A,B` and the resulting value inside *$orgunits* will be `["A", "B"]`. User-defined conversions can be supplied by implementing the `web.rest.Conversion` interface.

Matrix parameters
-----------------

[](#matrix-parameters)

This library supports parameters inside path segments, e.g. `https://example.com/api/trainings/status=COMPLETED;orgunits=A,B/authors`:

```
use web\rest\{Resource, Get, Matrix};

#[Resource('/api/trainings')]
class Trainings {

  #[Get('/{filter}/authors')]
  public function authors(#[Matrix] array $filter) {
    $authors= [];
    foreach ($this->repository->find($filter) as $training) {
      $authors[$training->author->id()]= $training->author;
    }
    return $authors;
  }
}
```

The resulting value inside *$filter* will be `["status" => "COMPLETED", "orgunits" => ["A", "B"]]`.

Return types
------------

[](#return-types)

Methods can return anything, which is then serialized and written to the response with a "200 OK" status. If you want greater control over the response, you can use the `web.rest.Response` class. It provides a fluent DSL for handling various scenarios.

Example:

```
return Response::created('/users/{id}', $id)->type('application/vnd.example.type-v2+json')->entity($user);
```

Creation:

- `Response::ok()` - 200 OK
- `Response::created([string $location])` - 201 Created, optionally with a *Location* header
- `Response::accepted()` - 202 Accepted
- `Response::noContent()` - 204 No content
- `Response::see(string $location)` - 302 Found and a *Location* header
- `Response::notModified()` - 304 Not modified
- `Response::notFound([string $message])` - 404 Not found and an optional message, which is serialized
- `Response::notAcceptable([string $message])` - 406 Not acceptable and an optional message, which is serialized
- `Response::error(int $code[, string $message])` - An error and an optional message, which is serialized
- `Response::status(int $code)` - Any other status code

Headers:

- `$response->type(string $mime)` will set the *Content-Type* header
- `$response->header(string $name, string $value)` will set a header with a given name and value

Body:

- `$response->entity(var $value)` will sent a value, serializing it
- `$response->stream(io.streams.InputStream $in[, int $size])` will stream a response
- `$response->body(string $bytes)` will write the given raw bytes to the response

Asynchronous invocation
-----------------------

[](#asynchronous-invocation)

The following code will run the upload function asynchronously, continuing to serve requests while file contents are being transmitted.

```
use io\Folder;
use web\rest\{Async, Post, Resource, Response};

#[Resource('/api')]
class Uploads {
  public function __construct(private Folder $folder) { }

  #[Post('/files')]
  public function upload(#[Request] $req) {
    return new Async(function() use($req) {
      if ($multipart= $req->multipart()) {

        foreach ($multipart->files() as $file) {
          yield from $file->transmit($this->folder);
        }
      }

      return Response::ok();
    });
  }
}
```

See also
--------

[](#see-also)

 - URL Shortener service

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 99.6% 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 ~79 days

Recently: every ~164 days

Total

35

Last Release

302d ago

Major Versions

v0.9.0 → v1.0.02018-10-06

v1.1.3 → v2.0.02020-04-10

v2.0.0 → v3.0.02020-10-09

v3.5.0 → v4.0.02023-07-25

v4.4.0 → v5.0.02025-05-04

PHP version history (3 changes)v0.1.0PHP &gt;=5.6.0

v2.0.0PHP &gt;=7.0.0

v5.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (252 commits)")[![johannes85](https://avatars.githubusercontent.com/u/470531?v=4)](https://github.com/johannes85 "johannes85 (1 commits)")

---

Tags

annotationsasynchronoushttpjsonmatrix-parametersno-xmlrest-apixp-frameworkmodulexp

### Embed Badge

![Health badge](/badges/xp-forge-rest-api/health.svg)

```
[![Health](https://phpackages.com/badges/xp-forge-rest-api/health.svg)](https://phpackages.com/packages/xp-forge-rest-api)
```

###  Alternatives

[xp-framework/compiler

XP Compiler

2026.0k9](/packages/xp-framework-compiler)

PHPackages © 2026

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