All Projects → mindplay-dk → Middleman

mindplay-dk / Middleman

Dead simple PSR-15 / PSR-7 middleware dispatcher

Projects that are alternatives of or similar to Middleman

middleland
Simple PSR-15 middleware dispatcher
Stars: ✭ 31 (-64.37%)
Mutual labels:  middleware, dispatcher
Lambda Middleware
A collection of middleware for AWS lambda functions.
Stars: ✭ 79 (-9.2%)
Mutual labels:  middleware
Thingsboard
Open-source IoT Platform - Device management, data collection, processing and visualization.
Stars: ✭ 10,526 (+11998.85%)
Mutual labels:  middleware
Handlers
A collection of useful middleware for Go HTTP services & web applications 🛃
Stars: ✭ 1,174 (+1249.43%)
Mutual labels:  middleware
Koatty
Koa2 + Typescript = Koatty. Use Typescript's decorator implement IOC and AOP.
Stars: ✭ 67 (-22.99%)
Mutual labels:  middleware
Cetus
Cetus is a high performance middleware that provides transparent routing between your application and any backend MySQL Servers.
Stars: ✭ 1,199 (+1278.16%)
Mutual labels:  middleware
Opencad Php
Open Source Computer Aided Dispatch System for Roleplay Communities.
Stars: ✭ 64 (-26.44%)
Mutual labels:  dispatcher
Koa2 Ratelimit
Rate-limiting middleware for Koa2 ES6. Use to limit repeated requests to APIs and/or endpoints such as password reset.
Stars: ✭ 81 (-6.9%)
Mutual labels:  middleware
Serve Static
Serve static files
Stars: ✭ 1,217 (+1298.85%)
Mutual labels:  middleware
Vos backend
vangav open source - backend; a backend generator (generates more than 90% of the code needed for big scale backend services)
Stars: ✭ 71 (-18.39%)
Mutual labels:  dispatcher
Bottlejs
A powerful dependency injection micro container for JavaScript applications
Stars: ✭ 1,171 (+1245.98%)
Mutual labels:  middleware
Express Joi Validation
validate express application inputs and parameters using joi
Stars: ✭ 70 (-19.54%)
Mutual labels:  middleware
Bbs node
node后端服务,node+express+mysql, 搭建token-权限-管理完整的web服务, 对应页面: https://www.lyh.red/admin \ https://www.lyh.red/bbs \ 接口地址https://www.lyh.red/apidoc/index.html
Stars: ✭ 78 (-10.34%)
Mutual labels:  middleware
Laravel Remember Uploads
Laravel Middleware and helper for remembering file uploads during validation redirects
Stars: ✭ 67 (-22.99%)
Mutual labels:  middleware
Go Web
A new Golang MVC Framework. Like Laravel... but faster!
Stars: ✭ 79 (-9.2%)
Mutual labels:  middleware
Nosurf
CSRF protection middleware for Go.
Stars: ✭ 1,131 (+1200%)
Mutual labels:  middleware
React Realization
some simple Realizations of react
Stars: ✭ 71 (-18.39%)
Mutual labels:  middleware
Fluentlyhttpclient
Http Client for .NET Standard with fluent APIs which are intuitive, easy to use and also highly extensible.
Stars: ✭ 73 (-16.09%)
Mutual labels:  middleware
Redirect Ssl
Connect/Express middleware to enforce https using is-https
Stars: ✭ 81 (-6.9%)
Mutual labels:  middleware
Fluxxan
Fluxxan is an Android implementation of the Flux Architecture that combines concepts from both Fluxxor and Redux.
Stars: ✭ 80 (-8.05%)
Mutual labels:  dispatcher

mindplay/middleman

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

Provides (optional) integration with a variety of dependency injection containers compatible with PSR-11.

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.

Usage

The constructor expects an array of PSR-15 MiddlewareInterface instances:

use mindplay\middleman\Dispatcher;

$dispatcher = new Dispatcher([
    new ErrorHandlerMiddleware(...)
    new RouterMiddleware(...),
    new NotFoundMiddleware(...),
]);

The Dispatcher implements the PSR-15 RequestHandlerInterface. This package only provides the middleware stack - to run a PSR-15 handler, for example in your index.php file, you need a PSR-15 host or a similar facility.

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

Anonymous Functions as Middleware

You can implement simple middleware "in place" by using anonymous functions in a middleware-stack, using a PSR-7/17 implementation such as nyholm/psr7:

use Psr\Http\Message\ServerRequestInterface;
use mindplay\middleman\Dispatcher;
use Nyholm\Psr7\Factory\Psr17Factory;

$factory = new Psr17Factory();

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

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

Dependency Injection via the Resolver Function

If you want to integrate with an IOC 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].