PHPackages                             baagee/php-onion - 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. baagee/php-onion

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

baagee/php-onion
================

PHP onion layer

v0.0.2(6y ago)1451MITPHPPHP &gt;=7.0

Since Mar 27Pushed 6y agoCompare

[ Source](https://github.com/baagee/php-onion)[ Packagist](https://packagist.org/packages/baagee/php-onion)[ RSS](/packages/baagee-php-onion/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)DependenciesVersions (3)Used By (1)

php-onion 洋葱结构
==============

[](#php-onion-洋葱结构)

php onion layer

可以利用它来做中间件，action之前做请求拦截，验证登陆，权限，或者action之后的一些逻辑

图示例：
----

[](#图示例)

图片源至koa的官方图

[![示例](https://camo.githubusercontent.com/ef1bcb7ac03c669743341cf57bde503b7eae5085d1e2be6ed6f23f1a93d69940/687474703a2f2f7777772e656e676b616e2e636f6d2f7573722f75706c6f6164732f323031382f30392f313437303235323236332e706e67)](https://camo.githubusercontent.com/ef1bcb7ac03c669743341cf57bde503b7eae5085d1e2be6ed6f23f1a93d69940/687474703a2f2f7777772e656e676b616e2e636f6d2f7573722f75706c6f6164732f323031382f30392f313437303235323236332e706e67)

使用示例：
-----

[](#使用示例)

### 首先定义要经过层

[](#首先定义要经过层)

```
/**
 * Class ReturnJson
 */
class ReturnJson extends \BaAGee\Onion\Base\LayerAbstract
{
    /**
     * @param Closure $next
     * @param         $data
     * @return mixed|void
     */
    protected function handler(Closure $next, $data)
    {
        print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL;
        $ret = $next($data);
        print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL;
        echo json_encode($ret, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;
    }
}

/**
 * Class CatchError
 */
class CatchError extends \BaAGee\Onion\Base\LayerAbstract
{
    /**
     * @param Closure $next
     * @param         $data
     * @return array|mixed
     */
    protected function handler(Closure $next, $data)
    {
        print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL;
        $ret = [
            'code'    => 0,
            'message' => ''
        ];
        try {
            $data        .= __CLASS__ . '; ';
            $ret['data'] = $next($data);
        } catch (Throwable $e) {
            $ret = [
                'code'    => $e->getCode(),
                'message' => $e->getMessage()
            ];
        }
        print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL;
        $ret['request_id'] = time();
        return $ret;
    }
}

/**
 * Class BLogic
 */
class BLogic extends \BaAGee\Onion\Base\LayerAbstract
{
    /**
     * @param Closure $next
     * @param         $data
     * @return mixed
     */
    protected function handler(\Closure $next, $data)
    {
        print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL;
        $data .= __CLASS__ . '; ';
        $ret  = $next($data);
        print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL;
        return $ret;
    }
}

/**
 * Class ALogic
 */
class ALogic extends \BaAGee\Onion\Base\LayerAbstract
{
    /**
     * @param Closure $next
     * @param         $data
     * @return mixed
     */
    protected function handler(Closure $next, $data)
    {
        print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL;
        $data .= __CLASS__ . "; ";
        // throw new Exception(__CLASS__ . ' Exception', 100);
        $ret = $next($data);
        print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL;
        return $ret;
    }
}
```

### 使用

[](#使用)

```
// 输入的数据
$input = 'input data; ';

// 经过的层  注意顺序，从前往后执行，然后从后往前一层一层返回
$through = [
    ReturnJson::class,
    CatchError::class,
    ALogic::class,
    BLogic::class
];
// 开始
(new \BaAGee\Onion\Onion())->send($input)->through($through)->then(function ($input) {
    return [
        'time'  => time(),
        'input' => $input
    ];
});
```

### 运行结果

[](#运行结果)

```
开始 ReturnJson 逻辑
开始 CatchError 逻辑
开始 ALogic 逻辑
开始 BLogic 逻辑
结束 BLogic 逻辑
结束 ALogic 逻辑
结束 CatchError 逻辑
结束 ReturnJson 逻辑
{
    "code": 0,
    "message": "",
    "data": {
        "time": 1553657337,
        "input": "input data; CatchError; ALogic; BLogic; "
    },
    "request_id": 1553657337
}

```

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

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

Total

2

Last Release

2484d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/22995500?v=4)[BaAGee](/maintainers/baagee)[@baagee](https://github.com/baagee)

---

Top Contributors

[![baagee](https://avatars.githubusercontent.com/u/22995500?v=4)](https://github.com/baagee "baagee (1 commits)")

---

Tags

phponion

### Embed Badge

![Health badge](/badges/baagee-php-onion/health.svg)

```
[![Health](https://phpackages.com/badges/baagee-php-onion/health.svg)](https://phpackages.com/packages/baagee-php-onion)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21422.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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