PHPackages                             fastd/middleware - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. fastd/middleware

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fastd/middleware
================

v8.0.0(1mo ago)318.8k35MITPHPPHP &gt;=8.2

Since Jan 9Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/fastdlabs/middleware)[ Packagist](https://packagist.org/packages/fastd/middleware)[ RSS](/packages/fastd-middleware/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (6)Versions (22)Used By (5)

FastD Middleware
================

[](#fastd-middleware)

[![Build Status](https://camo.githubusercontent.com/0f3ebfbc7fbfe2b96f9af5e1177c1ba4bbbaef75d4cc9d1ee95dee84d0155803/68747470733a2f2f7472617669732d63692e6f72672f66617374646c6162732f6d6964646c65776172652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fastdlabs/middleware)[![Support PSR15](https://camo.githubusercontent.com/02279f02b7f4b53d69889709429c17461bd86f4d4bbbbbedcddd21232107b974/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737570706f72742d70737231352d627269676874677265656e2e737667)](https://travis-ci.org/fastdlabs/middleware)[![Latest Stable Version](https://camo.githubusercontent.com/3eddc7af97411675e5ef74c383885f6775149a3d76b4706b75b48e7fc55e22cf/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f6d6964646c65776172652f762f737461626c65)](https://packagist.org/packages/fastd/middleware)[![Total Downloads](https://camo.githubusercontent.com/c5f44cda02324a4b0aba17dbd2052df6927b1f9d368b48400b7f6af256a8fd30/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f6d6964646c65776172652f646f776e6c6f616473)](https://packagist.org/packages/fastd/middleware)[![License](https://camo.githubusercontent.com/8e81614304b40e22c743c624ae73f5eacf477f064e399883ff803b5a10554e80/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f6d6964646c65776172652f6c6963656e7365)](https://packagist.org/packages/fastd/middleware)[![composer.lock](https://camo.githubusercontent.com/8cb8d4571fa420cbff84ecce1ffe151900e99b83cd368aae3e006136f3a1f789/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f6d6964646c65776172652f636f6d706f7365726c6f636b)](https://packagist.org/packages/fastd/middleware)

简介
--

[](#简介)

FastD Middleware 是一个实现了 PSR-15 HTTP 服务器中间件标准的轻量级中间件库。它基于 SplStack 实现了灵活的中间件栈管理，支持中间件的链式调用，适用于各种 PHP HTTP 应用程序。

环境依赖说明
------

[](#环境依赖说明)

- PHP &gt;= 8.2
- PSR-7 HTTP 消息接口实现
- PSR-15 HTTP 服务器中间件标准

基础使用说明
------

[](#基础使用说明)

### 安装

[](#安装)

```
composer require "fastd/middleware" -vvv
```

### 基本用法

[](#基本用法)

```
use FastD\Middleware\Dispatcher;
use FastD\Middleware\Middleware;
use FastD\Http\Request\ServerRequest;
use FastD\Http\Response\Text as Response;

// 创建一个中间件类
class ExampleMiddleware extends Middleware
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // 在请求处理前执行的逻辑
        $response = $handler->handle($request);
        // 在请求处理后执行的逻辑
        return $response;
    }
}

// 创建最终处理器
class FinalHandler extends Middleware
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        return (new Response())->withContents("Final response");
    }
}

// 创建调度器并添加中间件
$dispatcher = new Dispatcher();
$dispatcher->push(new FinalHandler());
$dispatcher->push(new ExampleMiddleware());

// 发起请求
$response = $dispatcher->dispatch(new ServerRequest('GET', '/'));
```

### 中间件栈操作

[](#中间件栈操作)

```
use FastD\Middleware\Dispatcher;

$dispatcher = new Dispatcher();

// 添加中间件到栈顶
$dispatcher->push($middleware);

// 从栈顶移除中间件
$dispatcher->pop();

// 添加中间件到栈底
$dispatcher->unshift($middleware);

// 从栈底移除中间件
$dispatcher->shift();

// 执行中间件链
$response = $dispatcher->dispatch($serverRequest);
```

文档详细引导
------

[](#文档详细引导)

### 核心组件

[](#核心组件)

FastD Middleware 包含以下核心组件：

1. **Dispatcher**: 负责管理中间件栈，提供 push/pop/unshift/shift 操作方法，并执行中间件链
2. **Middleware**: 抽象中间件基类，实现了 PSR-15 的 MiddlewareInterface 接口
3. **RequestHandler**: 请求处理器，封装回调函数并实现 RequestHandlerInterface 接口
4. **CallbackMiddleware**: 便捷中间件类，允许通过闭包创建中间件

### 中间件执行流程

[](#中间件执行流程)

1. Dispatcher 使用 SplStack 存储中间件
2. 通过递归解析中间件栈构建执行链
3. 从栈底开始依次执行中间件
4. 每个中间件可选择是否调用后续中间件
5. 执行完成后清空中间件栈

### 中间件开发

[](#中间件开发)

创建自定义中间件类：

```
use FastD\Middleware\Middleware;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;

class CustomMiddleware extends Middleware
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // 在请求处理前执行的逻辑

        // 调用下一个中间件
        $response = $handler->handle($request);

        // 在请求处理后执行的逻辑

        return $response;
    }
}
```

### 回调中间件

[](#回调中间件)

您可以使用 `CallbackMiddleware` 来快速创建中间件：

```
use FastD\Middleware\CallbackMiddleware;

$middleware = new CallbackMiddleware(function ($request, $handler) {
    // 前置逻辑
    $response = $handler->handle($request);
    // 后置逻辑
    return $response;
});
```

运行示例
----

[](#运行示例)

我们提供了一个示例文件来演示中间件的使用方法：

```
php example.php
```

该示例展示了：

- 基本中间件使用
- 回调中间件使用
- 中间件栈操作
- 复杂中间件链
- 异常处理

贡献
--

[](#贡献)

欢迎对项目感兴趣、愿意参与其中的开发者共同打造更好的 PHP 生态。

如果你有兴趣参与开发，可以尝试以下方式：

- 在你的项目中使用，将遇到的问题 [反馈](https://github.com/JanHuang/fastD/issues)。
- 提出更好的建议或功能需求。

License
-------

[](#license)

MIT License

###  Health Score

60

—

FairBetter than 98% of packages

Maintenance93

Actively maintained with recent releases

Popularity30

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 96.5% 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 ~180 days

Recently: every ~762 days

Total

20

Last Release

36d ago

Major Versions

v1.2.3 → v8.0.02026-05-28

PHP version history (2 changes)v1.0.0-rc1PHP &gt;=5.6

v8.0.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/94c2bc821caf23977e1c3deea85e3cbc9a73a632e1afaf778638f7fe9da1c42b?d=identicon)[JanHuang](/maintainers/JanHuang)

---

Top Contributors

[![JanHuang](https://avatars.githubusercontent.com/u/7090871?v=4)](https://github.com/JanHuang "JanHuang (82 commits)")[![RunnerLee](https://avatars.githubusercontent.com/u/7436388?v=4)](https://github.com/RunnerLee "RunnerLee (3 commits)")

---

Tags

middlewarephppsr-15

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/fastd-middleware/health.svg)

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

###  Alternatives

[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.5k1.5M88](/packages/mcp-sdk)[psr7-sessions/storageless

Storageless PSR-7 Session support

652405.7k8](/packages/psr7-sessions-storageless)[flarum/core

Delightfully simple forum software.

201.4M2.3k](/packages/flarum-core)[jaxon-php/jaxon-core

Jaxon is an open source PHP library for easily creating Ajax web applications

74149.4k30](/packages/jaxon-php-jaxon-core)[tomasnorre/crawler

Crawler extension for TYPO3

57428.7k1](/packages/tomasnorre-crawler)[xima/xima-typo3-frontend-edit

Frontend Edit - This extension provides an edit button for editors within frontend content elements.

1414.3k](/packages/xima-xima-typo3-frontend-edit)

PHPackages © 2026

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