jshannon63 / laravel-psr15-middleware
允许在 Laravel 中使用 PSR-15 兼容的中间件
Requires
- php: ^7.3
- nyholm/psr7: ^1.3
- psr/http-server-middleware: ^1.0
- symfony/psr-http-message-bridge: ^2
Requires (Dev)
- illuminate/config: ^5.5|^6|^7
- illuminate/container: ^5.5|^6|^7
- illuminate/http: ^5.5|^6|^7
- phpunit/phpunit: ^9.0
README
它做什么以及为什么
PHP-FIG 与 HTTP 消息接口(PSR-7)相关的标准已经存在一段时间了。HTTP 处理器(PSR-15)的标准于 2018 年 1 月 22 日获得批准。
Laravel 已经提供了从路由闭包或控制器方法中获取 PSR-7 请求对象的途径。Laravel 还允许从路由或控制器返回 PSR-7 响应对象。然而,有关中间件的新 PSR 标准并不意味着 Laravel 需要实现兼容的中间件堆栈……也不应该这样做。使用桥梁(如这个库)是完全可接受的,可以在不彻底改变底层框架的情况下在 Laravel 中采用 PSR-15 功能。
中间件是一件简单的事情,大部分中间件应该是瘦的,并且很容易编写。实际上,许多中间件不过是单行库,可以像创建和导入一样轻松地创建。然而,可重用的网络组件在应用程序/框架之间共享确实具有一些价值。PHP 社区中已经存在许多 PSR-15 中间件组件,其中一些可以提供一些价值。在 Laravel 中使用这些组件可能是一个优势。
出于任何可能获得的好处,即使是很小的可重用中间件逻辑,这个库也就因此被创建。
laravel-psr15-middleware 库(也称为 Psr15Middleware)是一个兼容 Laravel 的中间件,它创建了一个桥梁,将 PSR-7/PSR-15 接口与 Laravel 的中间件堆栈和基础 HTTP 消息对象连接起来。
安装后,您将能够使用此包在现有的 Laravel 中间件堆栈中集成的方式在 Laravel 中运行兼容的 PSR-15 中间件。
PSR 实现的理由。TL;DR
此库完全实现了 PSR-7(psr/http-message)消息对象接口。该接口通过 Zend Diactoros 具体的请求和响应对象实现。它还完全实现了新批准的 PSR-15(psr/http-server-middleware)中间件和(psr/http-server-handler)请求处理器接口。此库使用 Symfony PSR-7 Bridge 在基础和 PSR-7 消息对象之间进行双向转换。
安装
在您的 Laravel 项目文件夹中,使用 composer 安装此包。如果您使用 Laravel 5.5 或更高版本,服务提供者注册将自动发生。
composer require jshannon63/laravel-psr15-middleware
然后使用 artisan 发布包的配置资源
php artisan vendor:publish Which provider or tag's files would you like to publish?: [0] Publish files from all providers and tags listed below [1] Provider: Fideloper\Proxy\TrustedProxyServiceProvider [2] Provider: Illuminate\Mail\MailServiceProvider [3] Provider: Illuminate\Notifications\NotificationServiceProvider [4] Provider: Illuminate\Pagination\PaginationServiceProvider [5] Provider: Jshannon63\Psr15Middleware\Psr15MiddlewareServiceProvider [6] Tag: laravel-mail [7] Tag: laravel-notifications [8] Tag: laravel-pagination > choose the Psr15MiddlewareServiceProvider
这就完成了!现在您可以为 PSR-15 中间件进行配置和运行。默认的配置 /config/psr15middleware.php
包含启用的 exampleMiddleware 以进行演示。您需要禁用所有示例并添加自己的中间件类,如下所示。
使用
将您的 PSR-15 兼容中间件添加到 /config/psr15middleware.php
配置文件中。
- 在
app/Http/Middleware/Kernel.php
文件中声明 PSR-15 中间件不是必需的,正如通常所做的那样。Psr15Middleware 将自动注册自己及其中间件,通过将它们推送到 Laravel 中间件堆栈中。 - 配置条目是数组,可以包含类名、可调用对象或对象,如下面的示例所示。每个条目有两个附加参数,跟在中间件声明之后
- "prepend" 或 "append" 将决定你的中间件是放置在中间件堆栈的开头还是结尾。
- "before"、"after" 和 "terminable" 指定了中间件类型。"before" 中间件在应用程序处理请求之前运行。"after" 中间件在应用程序处理完请求后运行,但在响应发送到浏览器之前。"terminable" 中间件在浏览器收到响应后运行,通常用于需要访问请求和/或响应对象的维护任务。
- 额外的部分用于别名($routeMiddleware)和组($middlewareGroups),它们与
app\Http\Middleware\Kernel.php
文件中的特殊路由中间件组紧密相关。 - 如果您喜欢,可以添加新的组(例如,如自定义所示)。
- 可以在配置中将构造函数参数传递给声明为可调用的中间件或对象。所有 PSR-15 中间件构造函数都将被视为可变参数函数,因此可以接受任何数量的构造函数参数。注意:这些构造函数参数也可以作为 Laravel 中间件路由参数传递。有关此功能的更多信息,请参阅 Laravel 文档。
<?php return [ 'middleware' => [ [\Jshannon63\Psr15Middleware\exampleMiddleware::class, 'append', 'before'], [ function() { return new \Jshannon63\Psr15Middleware\exampleMiddleware('Lovin', 'Laravel'); }, 'prepend', 'after' ], [ (new \Jshannon63\Psr15Middleware\exampleMiddleware('PSR-15','Rocks')), 'append', 'after' ] ], 'groups' => [ 'web' => [ ], 'api' => [ ], 'custom' => [ ], ], 'aliases' => [ 'psr15' => [ (new \Jshannon63\Psr15Middleware\exampleMiddleware('Aliased','Middleware')), 'prepend', 'after' ] ] ];
您的 PSR-15 兼容中间件必须具有以下签名
// your namespace here use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class exampleMiddleware implements MiddlewareInterface { // Your constructor will be treated as a variadic function // and parameters may be passed either as a middleware route // parameter or as defined in the /config/psr15middleware.php // config file. You can read more about middleware parameters // in the Laravel documentation. public function __construct() { // if needed } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { // process any request manipulations here before the handler. // remember that only "before" middlewares have access to // the request object before the application acts on it. // the handler will ensure the next middleware will see any // changes to the request object. $response = $handler->handle($request); // response actions go here after the handler provides // you with a response object. keep in mind that any // "before" middlewares will only have access to a mock // response object and any updates will be lost. // "terminable" middlewares are run after the response has // been sent back to the browser. they will receive the // request object passed into this method and will get // a copy of the response object from the handler. // return the reponse object here. return $response; } }
执行流程
所有 PSR-15 中间件都由 PSR15Middleware 子系统完全封装和管理。在启动时,Psr15Middleware 将为每个 PSR-15 中间件绑定一个包装对象,使其在 Laravel 中看起来是原生的。然后,根据您的配置参数将这些对象放入 Laravel 中间件堆栈中。中间件本身只有在需要时才会被实例化。Laravel 将根据系统优先级和通过 Psr15Middleware 注册 PSR-15 中间件期间进行修改来执行中间件。
此外,请注意,由于 Psr15Middlware 在 PSR-7 消息对象上操作,PSR-15 中间件将无法访问 Laravel/Symfony 基础消息对象的特定属性和方法。
中间件来源
对于某些需要使用的 PSR-15 中间件,请参阅 middlewares/psr15middlewares。
贡献
如果您想贡献,请参阅 CONTRIBUTING.md。