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
Requires
- php: >=7.2
- webiik/data: ^1
This package is auto-updated.
Last update: 2024-10-29 05:16:52 UTC
README
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();