PHPackages                             cxx/tp5-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. [Framework](/categories/framework)
4. /
5. cxx/tp5-middleware

ActiveLibrary[Framework](/categories/framework)

cxx/tp5-middleware
==================

tp5-middleware,中间件,middleware,thinkphp5

1.2.0(4y ago)017MITPHP

Since Jun 10Pushed 4y ago1 watchersCompare

[ Source](https://github.com/cxx2320/tp5-middleware)[ Packagist](https://packagist.org/packages/cxx/tp5-middleware)[ RSS](/packages/cxx-tp5-middleware/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (6)Used By (0)

基于tp5版本的中间件

主要特性
----

[](#主要特性)

- 代码无侵入性
- 更细粒度的操作

安装使用
----

[](#安装使用)

`composer require cxx/tp5-middleware`

1. 行为绑定，在 `application\tags.php` 中添加

```
return [
    // 应用初始化
    'app_init'     => [],
    // 应用开始
    'app_begin'    => [],
    // 模块初始化
    'module_init'  => [
        // !! 此行代码
        'Cxx\Tp5Middleware\MiddlewareHandle::moduleInit'
    ],
    // 操作开始执行
    'action_begin' => [],
    // 视图内容过滤
    'view_filter'  => [],
    // 日志写入
    'log_write'    => [],
    // 应用结束
    'app_end'      => [
        // !! 此行代码
        'Cxx\Tp5Middleware\MiddlewareHandle::appEnd'
    ],
    'response_end' => [
        // !! 此行代码
        'Cxx\Tp5Middleware\MiddlewareHandle::responseEnd'
    ]
];
```

2. 创建中间件(三个方法都是可选的,类写在哪里都无所谓，只要能被加载)

- 命令行

在 `applocation\command.php` 添加

```
    return [
        Cxx\Tp5Middleware\MiddlewareCommand::class
    ];
```

使用命令行直接创建 `php think make:middleware Test` ,会生成 `application/middleware/Test.php` 文件

- 手动创建

```
class Middle1
{
    /**
     * 前置方法
     * @param \think\Request $request
     */
    public function before($request){}

    /**
     * 后置方法
     * @param \think\Response $response
     */
    public function after($response){}

    /**
     * 响应结束方法
     * 在此的任何输出都不会响应给浏览器，参考 thinkphp/library/think/Response.php 128行代码
     * @param \think\Request $request
     * @param \think\Response $response
     */
    public function end($request, $response){}
}
```

3. 使用(创建 `application\extra\middleware.php` 文件)

```
return [
    // 全局中间件
    'global' => [
        Middle1::class
    ],
    // 模块中间件
    'module' => [
        // 模块名称
        'index' => [
            Middle1::class
        ]
    ],
    // 控制器中间件
    'controller' => [
        // 相应的控制器
        Index::class => [
            Middle1::class
        ]
    ],
];
```

控制器中间件的另一种定义方式

```
protected $middleware = [
    Middle1::class => ['except' => ['hello'] ],
    Middle2::class => ['only' => ['hello'] ],
];
```

调用顺序
----

[](#调用顺序)

前置方法-&gt;后置方法

`global -> module -> controller -> (核心代码) -> controller -> module -> global`

响应结束方法

`global -> module -> controller`

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

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 ~7 days

Total

5

Last Release

1773d ago

Major Versions

0.0.2 → 1.0.02021-06-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/b687edf153814a0660b9d1104ebe92b472673860c2786fa475e995b4d7e31268?d=identicon)[CXX](/maintainers/CXX)

---

Top Contributors

[![cxx2320](https://avatars.githubusercontent.com/u/34790068?v=4)](https://github.com/cxx2320 "cxx2320 (8 commits)")

### Embed Badge

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

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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