• Stars
    star
    34
  • Rank 741,026 (Top 16 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple PSR-15 middleware dispatcher

Middleland

Latest Version on Packagist Software License Testing Quality Score

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.

More Repositories

1

Embed

Get info from any web service or page
PHP
2,045
star
2

psr7-middlewares

[DEPRECATED] Collection of PSR-7 middlewares
PHP
672
star
3

node-sketch

💎 Javascript library to manipulate sketch files
JavaScript
306
star
4

imagecow

PHP library to manipulate and generate responsive images
PHP
240
star
5

simple-crud

PHP library to provide magic CRUD in MySQL/Sqlite databases with zero configuration
PHP
237
star
6

form-manager

PHP library to create and validate html forms
PHP
152
star
7

jquery-cheatsheet

jQuery interactive cheatsheet
CSS
137
star
8

awesome-design

A collection of open resources for web designers
97
star
9

social-links

Simple library to count shares and generate share buttons
PHP
95
star
10

env

Simple library to read environment variables and convert to simple types.
PHP
81
star
11

bookmarklets

Simple framework to build bookmarklets easily
JavaScript
68
star
12

keep-a-changelog

Node & Deno package to parse and generate changelogs
TypeScript
56
star
13

deno-cheatsheet

Deno cheat sheet with APIs and tools
CSS
53
star
14

stylecow

[deprecated] CSS preprocessor written in PHP
PHP
46
star
15

vento

🌬 A template engine for Deno & Node
TypeScript
44
star
16

jQuery.media

jQuery based library to manage video and audio html5 elements
JavaScript
30
star
17

php-server-manager

Manage PHP built-in server in node
JavaScript
28
star
18

semantic-html

Collection of semantic HTML use cases
23
star
19

inline-svg

Insert svg in the html so you can use css to change the style
PHP
19
star
20

css-style-guide

My own css style guide
HTML
15
star
21

nginx-snippets

Custom snippets for nginx
14
star
22

html-parser

Simple utility to parse html strings to DOMDocument
HTML
13
star
23

fly-crud

Basic crud system built on top of flysystem
PHP
12
star
24

nodedeno

Script to convert Node libraries to Deno
JavaScript
11
star
25

folk

Universal CMS to use with any web
PHP
10
star
26

html

PHP library to generate HTML code
PHP
9
star
27

gpm

Git-based package manager for Deno
TypeScript
9
star
28

uploader

Basic php library to upload files
PHP
7
star
29

d.js

DOM manipulation micro library (~4Kb)
JavaScript
7
star
30

dbin

Library to download binary files from GitHub releases detecting the correct platform.
TypeScript
7
star
31

typofixer

Fix microtypography issues in html code
PHP
7
star
32

server

Simple class to emulate Apache's "mod_rewrite" functionality from the built-in PHP web server
PHP
5
star
33

server-style-guide

Step-by-step instructions to install and configure a web server
CSS
5
star
34

psr7-unitesting

Test your psr-7 http messages easily
PHP
5
star
35

polyfills

List of polyfills to use modern things safely
4
star
36

awesome-talks

Collection of design talks in galician and spanish
4
star
37

zume

A static-site generator built on top of gulp.
JavaScript
4
star
38

memes-da-vida

Xerador de memes con debuxos de Castelao
HTML
3
star
39

vscode-vento

Vento for Visual Studio Code
3
star
40

fol

Base app to build websites
PHP
3
star
41

ha

Código público de historia-arte.com
PHP
3
star
42

jose

Feed reader
PHP
3
star
43

gist-runner

Simple script to run github gist files in localhost
JavaScript
3
star
44

history-navigator

Minimalist js library to navigate across the browser history
JavaScript
3
star
45

jquery.lazyscript

Simple jquery plugin to load or transform elements in lazy mode
HTML
3
star
46

simple-crud-extra-fields

Extra fields for simple-crud package
PHP
2
star
47

view-helpers

Collection of useful functions to use in your templates
PHP
2
star
48

form-manager-bootstrap

Simple FormManager extension to create bootstrap-like forms
PHP
2
star
49

php-cs-fixer-config

My own custom php-cs-fixer config
PHP
2
star
50

matomo-tracker

Generate Matomo tracker urls that you can use to insert tracking images in your site
PHP
2
star
51

how-to-do-it

cli utility to help me to remember other cli commands
JavaScript
1
star
52

netlify_cms_config

Netlify CMS config generator
TypeScript
1
star
53

chipi-client

JavaScript
1
star
54

domplates

Easy HTML <template>
JavaScript
1
star
55

icona

1 svg + 1 css = multiple icons
CSS
1
star
56

designtokens

A Deno/Node library to parse, manipulate and transform design tokens
TypeScript
1
star