PHPackages                             constanze-standard/request-handler - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. constanze-standard/request-handler

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

constanze-standard/request-handler
==================================

The PSR-15 request handler.

1.0.1(6y ago)0381Apache-2.0PHPPHP &gt;=7.1.0

Since Sep 8Pushed 6y agoCompare

[ Source](https://github.com/constanze-standard/request-handler)[ Packagist](https://packagist.org/packages/constanze-standard/request-handler)[ RSS](/packages/constanze-standard-request-handler/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (1)

Constanze standard request handler
==================================

[](#constanze-standard-request-handler)

[![GitHub license](https://camo.githubusercontent.com/1e07db00d547cb8acb3caf3b24ddbb417ed0a2319a5633cf69b1528ba0f49f1e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d417061636865253230322d626c7565)](https://github.com/constanze-standard/request-handler/blob/master/LICENSE)[![Coverage 100%](https://camo.githubusercontent.com/964e6727b820bf4bd13789e84922b6359a9493323aa7d455ec675f1cf403209c/68747470733a2f2f696d672e736869656c64732e696f2f617a7572652d6465766f70732f636f7665726167652f7377656c6c6162792f6f70656e736f757263652f32352e737667)](https://github.com/constanze-standard/request-handler)

PSR-15 请求处理程序
-------------

[](#psr-15-请求处理程序)

The PSR-15 request handler.

简介
--

[](#简介)

这时是一个遵循 PSR-15 标准的请求与中间件处理器，用于构建中间件应用程序，并派发请求。

安装
--

[](#安装)

> composer require constanze-standard/request-handler

开始使用
----

[](#开始使用)

Request handler 需要和 PSR-7 标准结合使用，我们的示例使用 `guzzlehttp/psr7`, 你可能需要首先安装它，当然，你也可以选择使用自己熟悉的 `psr/http-message` 实现方案。

首先需要定义一个 `PSR-15 RequestHandler` 作为应用程序的核心初始化派发器 `Dispatcher`。

```
use ConstanzeStandard\RequestHandler\Dispatcher;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class CoreHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return new Response(200, [], json_encode(
            $request->getAttribute('order')
        ));
    }
}

$dispatcher = new Dispatcher(new CoreHandler());
```

现在我们可以向派发器添加中间件了，中间件必须实现 `psr/http-server-middleware` 提供的 `\Psr\Http\Server\MiddlewareInterface`.

```
use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class Middleware implements MiddlewareInterface
{
    public function __construct($order)
    {
        $this->order = $order;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $order = $request->getAttribute('order');
        array_push($order, 'middleware' . $this->order);
        $request = $request->withAttribute('order', $order);
        return $handler->handle($request);
    }
}
```

调用 `addMiddleware` 方法，将中间件按照从内向外的顺序添加到派发器中。

```
$dispatcher->addMiddleware(new Middleware(1));
$dispatcher->addMiddleware(new Middleware(2));
```

DIspatcher 在内部构建了一个栈结构的中间件应用，当添加一个中间件时，相当于向栈的外层做了 push 操作。

```
*---------------------*
|     middleware2     |
|  *---------------*  |
|  |  middleware1  |  |
|  |   *-------*   |  |
|  |   |       |   |  |    栈结构的中间件
|  |   | core  |   |  |
|  |   |       |   |  |
|  |   *-------*   |  |
|  *---------------*  |
*---------------------*

```

之后，我们触发 request handler, 通过 `Dispatcher::handle` 方法，自动的由栈的外层向内层依次调用，最终得到核心 handler 所返回的 response 对象。

```
use GuzzleHttp\Psr7\ServerRequest;

$request = new ServerRequest('GET', '/user');
$request->withAttribute('order', []);
$response = $dispatcher->handle($request);

echo $response->getBody()->getContents();
// 输出 ["middleware2","middleware1"]
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

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

Total

2

Last Release

2440d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f4cfe2ff9c9a5fe469dd2701b7c22132e5b5a9e053a32c54d7ae90cfe565ef3?d=identicon)[Alex Layton](/maintainers/Alex%20Layton)

---

Top Contributors

[![alex-1900](https://avatars.githubusercontent.com/u/49949411?v=4)](https://github.com/alex-1900 "alex-1900 (5 commits)")

---

Tags

psrmiddlewarestackpsr-15request-handler

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/constanze-standard-request-handler/health.svg)

```
[![Health](https://phpackages.com/badges/constanze-standard-request-handler/health.svg)](https://phpackages.com/packages/constanze-standard-request-handler)
```

###  Alternatives

[mezzio/mezzio

PSR-15 Middleware Microframework

3883.6M97](/packages/mezzio-mezzio)[idealo/php-middleware-stack

Implementation of HTTP Middleware PSR-15 specification

318.9k](/packages/idealo-php-middleware-stack)[mezzio/mezzio-authentication

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)[jimtools/jwt-auth

PSR-15 JWT Authentication middleware, A replacement for tuupola/slim-jwt-auth

20142.3k3](/packages/jimtools-jwt-auth)

PHPackages © 2026

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