hannesvdvreken/psr7-middlewares

此包已被废弃,不再维护。没有建议的替代包。

0.1.0 2015-09-09 20:46 UTC

This package is auto-updated.

Last update: 2022-02-01 12:51:10 UTC


README

不再维护

创建可调用的中间件。签名

function (RequestInterface $request, ResponseInterface $response, callable $next) {
    // Do something with the request before passing it on.
    ...

    // Pass on to next middleware.
    $response = $next($request, $response);

    // Do something with the response after the next middleware has been called.
    ...

    // Return ultimate response object.
    return $response;
}

PSR-7 中间件

Latest Version Software License Build Status Coverage Status Quality Score

用于 PSR-7 HTTP 消息对象的堆叠中间件。

安装

composer require hannesvdvreken/psr7-middlewares

描述

此包是一个小型库,帮助您构建装饰过的中间件数组。

有 2 种类型的中间件

一个 Kernel,它是一个可以将请求和响应对象传递给下一层的中间件。一个 Core 对象通常是中间件列表的最后一层。核心将始终返回一个 PSR-7 ResponseInterface 对象,而不会将给定的 RequestInterface 对象传递给下一层。它永远不会设置下一个中间件。

Builder 对象有助于创建一个由指定层组成的组合 Core。请注意,builder 对象是不可变的:因此,每次 pushunshift 调用后都会返回一个不同的已修改对象。

use Psr7Stack\Builder;

$builder = new Builder();

$session = new SessionMiddleware();
$throttle = new TrottleMiddleware();
$app = new App();

$builder = $buider->push($session)->push($throttle)->push($app);

$stack = $builder->resolve();

返回的 Stack 对象也可以使用静态工厂方法 create 创建。

use Psr7Stack\Stack;

$stack = Stack::create([$session, $throttle, $app]);

Stack 对象本身也是一个 Core 中间件,因此它可以用在不同的中间件组合中。这就是如何通过中间件的不同层发送请求对象的方式

$psrResponse = $stack->handle($psrRequest);

扩展

创建自己的 Core

use Psr7Stack\Core;
use Psr\Http\Message\RequestInterface;

class App implements Core
{
    /**
     * @param \Psr\Http\Message\RequestInterface $request
     *
     * @request \Psr\Http\Message\ResponseInterface
     */
    public function handle(RequestInterface $request)
    {
        // Call the router and return the response.
        return $response;
    }
}

创建 Kernel

use Psr7Stack\Kernel;
use Psr7Stack\Traits\NextCore;

class SessionMiddleware implements Kernel
{
    // Use trait to implement the setNextCore method.
    use NextCore;

    /**
     * @param \Psr\Http\Message\RequestInterface $request
     *
     * @request \Psr\Http\Message\ResponseInterface
     */
    public function handle(RequestInterface $request)
    {
        // Call the next core and return the response.
        $response = $this->next->handle($request);

        // Do something with the response and return it.
        ...

        return $response;
    }
}

中间件

现有的

  • League/route 应用程序核心

更多中间件的想法

  • 机器人中间件。返回特定环境的 robots.txt 文件。
  • 任何类型的框架应用程序
  • 节流中间件
  • CORS 中间件
  • 缓存中间件
  • 基于 IP 的防火墙中间件
  • 记录中间件

贡献

欢迎贡献。有关如何贡献的信息,请参阅贡献文件

许可

MIT 许可证 (MIT)。有关更多信息,请参阅许可文件