承接 oscarotero/middleland 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oscarotero/middleland

最新稳定版本:v1.0.3

Composer 安装命令:

composer require oscarotero/middleland

包简介

PSR-15 middleware dispatcher

README 文档

README

Latest Version on Packagist Software License

Simple (but powerful) PSR-15 middleware dispatcher:

Requirements

Example

use Middleland\Dispatcher; $middleware = [ new Middleware1(), new Middleware2(), new Middleware3(), // A dispatcher can be used to group middlewares new Dispatcher([ new Middleware4(), new Middleware5(), ]), // You can use closures function ($request, $next) { $response = $next->handle($request); return $response->withHeader('X-Foo', 'Bar'); }, // Or use a string to create the middleware on demand using a PSR-11 container 'middleware6' // USE AN ARRAY TO ADD CONDITIONS: // This middleware is processed only in paths starting by "/admin" ['/admin', new MiddlewareAdmin()], // This is processed in DEV [ENV === 'DEV', new MiddlewareAdmin()], // Use callables to create other conditions [ function ($request) { return $request->getUri()->getScheme() === 'https'; }, new MiddlewareHttps() ], // There are some matchers included in this library to create conditions [ new Pattern('*.png'), new MiddlewareForPngFiles() ], //And use several for each middleware [ ENV === 'DEV', new Pattern('*.png'), new MiddlewareForPngFilesInDev() ], ]; $dispatcher = new Dispatcher($middleware, new Container()); $response = $dispatcher->dispatch(new Request());

Matchers

As you can see in the example above, you can use an array of "matchers" to filter the requests that receive middlewares. You can use callables, instances of Middleland\Matchers\MatcherInterface or booleans, but for comodity, the string values are also used to create Middleland\Matchers\Path instances. The available matchers are:

Name Description Example
Path Filter requests by base path. Use exclamation mark for negative matches new Path('/admin'), new Path('!/not-admin')
Pattern Filter requests by path pattern. Use exclamation mark for negative matches new Pattern('*.png') new Pattern('!*.jpg')
Accept Filter requests by Accept header. Use exclamation mark for negative matches new Accept('text/html') new Accept('!image/png')

How to create matchers

Just use a callable or an instance of the Middleland\Matchers\MatcherInterface. Example:

use Middleland\Matchers\MatcherInterface; use Psr\Http\Message\ServerRequestInterface; class IsAjax implements MatcherInterface { public function __invoke(ServerRequestInterface $request): bool { return $request->getHeaderLine('X-Requested-With') === 'xmlhttprequest'; } }

Please see CHANGELOG for more information about recent changes.

The MIT License (MIT). Please see LICENSE for more information.

统计信息

  • 总下载量: 79.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 0
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固