The Middleware Interface
All middlewares must implement theapivalk\apivalk\Middleware\MiddlewareInterface. This interface defines a single method:
$request: The current request object.$controllerClass: The fully qualified class name of the target controller.$next: The next middleware in the stack or the final controller.
Middleware Stack
TheMiddlewareStack class manages the execution of middlewares. Middlewares are added to the stack and then processed in the order they were added.
Onion Execution Pattern
WhenMiddlewareStack::handle() is called, it wraps the controller and all middlewares into a nested closure.
- Request Phase: The first middleware executes its logic before calling
$next($request). - Next Call: This continues until the last middleware calls the actual controller.
- Response Phase: The controller returns a response, which then travels back through the middleware stack in reverse order, allowing each middleware to modify the response if needed.
Configuration
Middlewares are typically configured via theApivalkConfiguration object during the bootstrapping phase.