承接 vakata/middleman 相关项目开发

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

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

vakata/middleman

Composer 安装命令:

composer require vakata/middleman

包简介

PSR-15 middleware dispatcher. Let's stop trying to make this complicated.

README 文档

README

Dead simple PSR-15 / PSR-7 middleware dispatcher.

Provides (optional) integration with a variety of dependency injection containers compliant with container-interop.

To upgrade between major releases, please see UPGRADING.md.

PHP Version Build Status Code Coverage Scrutinizer Code Quality

A growing catalog of PSR-15 middleware-components is available from github.com/middlewares.

You can implement simple middleware "in place" by using anonymous functions in a middleware-stack:

use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

$dispatcher = new Dispatcher([
    function (ServerRequestInterface $request, callable $next) {
        return $next($request); // delegate control to next middleware
    },
    function (ServerRequestInterface $request) {
        return (new Response())->withBody(...); // abort middleware stack and return the response
    },
    // ...
]);

$response = $dispatcher->dispatch($request);

For simplicity, the middleware-stack in a Dispatcher is immutable - if you need a stack you can manipulate, array, ArrayObject, SplStack etc. are all fine choices.

To implement reusable middleware components, you should implement the PSR-15 MiddlewareInterface.

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\ServerMiddleware\MiddlewareInterface;

class MyMiddleware implements MiddlewareInteface
{
    public function process(ServerRequestInterface $request, DelegateInterface $delegate) {
        // ...
    }
}

If you want to integrate with a DI container you can use the ContainerResolver - a "resolver" is a callable which gets applied to every element in your middleware stack, with a signature like:

function (string $name) : MiddlewareInterface

The following example obtains middleware components on-the-fly from a DI container:

$dispatcher = new Dispatcher(
    [
        RouterMiddleware::class,
        ErrorMiddleware::class,
    ],
    new ContainerResolver($container)
);

If you want the Dispatcher to integrate deeply with your framework of choice, you can implement this as a class implementing the magic __invoke() function (as ContainerResolver does) - or "in place", as an anonymous function with a matching signature.

If you want to understand precisely how this component works, the whole thing is just one class with a few lines of code - if you're going to base your next project on middleware, you can (and should) understand the whole mechanism.

Middleware?

Middleware is a powerful, yet simple control facility.

If you're new to the concept of middleware, the following section will provide a basic overview.

In a nutshell, a middleware component is a function (or MiddlewareInterface instance) that takes an incoming (PSR-7) RequestInterface object, and returns a ResponseInterface object.

It does this in one of three ways: by assuming, delegating, or sharing responsibility for the creation of a response object.

1. Assuming Responsibility

A middleware component assumes responsibility by creating and returning a response object, rather than delegating to the next middleware on the stack:

use Zend\Diactoros\Response;

function ($request, $next) {
    return (new Response())->withBody(...); // next middleware won't be run
}

Middleware near the top of the stack has the power to completely bypass middleware further down the stack.

2. Delegating Responsibility

By calling $next, middleware near the top of the stack may choose to fully delegate the responsibility for the creation of a response to other middleware components further down the stack:

function ($request, $next) {
    if ($request->getMethod() !== 'POST') {
        return $next($request); // run the next middleware
    } else {
        // ...
    }
}

Note that exhausting the middleware stack will result in an exception - it's assumed that the last middleware component on the stack always produces a response of some sort, typically a "404 not found" error page.

3. Sharing Responsibility

Middleware near the top of the stack may choose to delegate responsibility for the creation of the response to middleware further down the stack, and then make additional changes to the returned response before returning it:

function ($request, $next) {
    $result = $next($request); // run the next middleware

    return $result->withHeader(...); // then modify it's response
}

The middleware component at the top of the stack ultimately has the most control, as it may override any properties of the response object before returning.

vakata/middleman 适用场景与选型建议

vakata/middleman 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 881 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 vakata/middleman 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vakata/middleman 我们能提供哪些服务?
定制开发 / 二次开发

基于 vakata/middleman 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 881
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0
  • 更新时间: 2018-04-16