webiik/middleware

The Middleware is middleware launcher that allows automatic constructor DI and sending custom data among middleware.

1.0 2019-03-28 17:48 UTC

This package is auto-updated.

Last update: 2024-04-29 03:54:18 UTC


README

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f77656269696b2f77656269696b2e737667 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d322d627269676874677265656e2e737667

Middleware

The Middleware is middleware launcher that allows automatic constructor DI and sending custom data among middleware.

Installation

composer require webiik/middleware

Example

$middleware = new \Webiik\Middleware\Middleware($container, $data);
$middleware->add('MwTest:run', ['foo' => 'bar']);
$middleware->run();

Writing Middleware

Every middleware has to be a class with at least one public method with the following parameters \Webiik\Data\Data $data, callable $next. Every class can contain more methods with the mentioned parameters.

class MwTest
{
    public function run(\Webiik\Data\Data $data, callable $next)
    {
        // Get middleware data
        $mwData = $data->getAll(); // Array([foo] => [bar])
         
        // Change middleware data 
        $data->set('foo', 'foo');
        
        // Launch next middleware
        $next();
        
        // Continue current middlware...
    }
}

You can also use constructor DI to inject services from container to middleware. Read more about DI and container.

Adding

add

add(string $controller, $data = []): void

add() ads middleware to queue.

Parameters:

  • controller string representation of controller and method to be initiated e.g. controllerName:methodName
  • data a key-value array of data to be injected during the middleware initiation
$middleware->add('MwTest:run');

Launch

run

run(): void

run() runs all middleware in queue.

$middleware->run();

Resources