asb/middleware-pipe

This is the pattern that Laravel uses for its HTTP Middleware as well as its internal Pipeline; each Middleware can execute its own logic before and after executing the next step ($next).

Maintainers

Package info

github.com/stormyboy68/middlewarePipe

pkg:composer/asb/middleware-pipe

Transparency log

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-07-17 18:12 UTC

This package is auto-updated.

Last update: 2026-08-02 17:46:23 UTC


README

A lightweight and framework-independent middleware pipeline for PHP.

This package allows you to execute one or more middleware classes before and after calling a class method. It follows the same pipeline pattern used by Laravel's middleware system but can be used in any PHP project.

Features

  • 🚀 Simple and lightweight
  • 📦 No framework dependency
  • 🔗 Supports multiple middleware
  • 🔄 Execute code before and after the target method
  • 🧩 Supports both object and static method calls
  • âš¡ PHP 8.0+

Requirements

  • PHP 8.0 or higher

Installation

Install via Composer:

composer require your-vendor/middleware-pipeline

Middleware Structure

Every middleware must implement a handle() method.

Example:

class LoggerMiddleware
{
    public function handle($request, $next)
    {
        // Before

        $result = $next($request);

        // After

        return $result;
    }
}

The $next callback continues the execution of the pipeline.

Using the Trait

Add the trait to your class.

use ASB\Middleware\MiddlewarePipeline;

class UserService
{
    use MiddlewarePipeline;

    public function save($name)
    {
        return "User {$name} saved.";
    }
}

Object Example

$service = new UserService();

$result = $service
    ->middleware([
        LoggerMiddleware::class,
        AuthMiddleware::class,
    ])
    ->save("John");

Execution order:

LoggerMiddleware
    ↓
AuthMiddleware
    ↓
save()
    ↑
AuthMiddleware
    ↑
LoggerMiddleware

Static Example

class UserService
{
    use MiddlewarePipeline;

    public static function save($name)
    {
        return "User {$name} saved.";
    }
}

$result = UserService::middlewared([
    LoggerMiddleware::class,
])->save("John");

How It Works

The package builds a middleware pipeline using nested closures.

Calling

$pipeline($params);

starts the execution.

Execution flow:

Request
    │
    â–¼
Middleware A
    │
    â–¼
Middleware B
    │
    â–¼
Middleware C
    │
    â–¼
Target Method
    │
    â–²
Middleware C
    â–²
Middleware B
    â–²
Middleware A
    │
    â–¼
Response

Each middleware can:

  • Execute code before the target method.
  • Execute code after the target method.
  • Modify the returned value.
  • Stop the pipeline without calling $next.

Example Middleware

class LoggerMiddleware
{
    public function handle($request, $next)
    {
        echo "Before\n";

        $result = $next($request);

        echo "After\n";

        return $result;
    }
}

Multiple Middlewares

$service
    ->middleware([
        AuthMiddleware::class,
        ValidationMiddleware::class,
        LoggerMiddleware::class,
    ])
    ->save();

Execution order:

Auth
    ↓
Validation
    ↓
Logger
    ↓
save()
    ↑
Logger
    ↑
Validation
    ↑
Auth

License

This project is open-sourced software licensed under the MIT License.

Author

Developed by Abouzar Rostami.