Middleware Concept
Middleware in the Instructor HTTP client API follows the pipeline pattern, where each middleware component gets a chance to process the request before it’s sent and the response after it’s received. The middleware chain works like this:- Your application creates a request
- The request passes through each middleware (in the order they were added)
- The last middleware passes the request to the HTTP driver
- The driver sends the request to the server and receives a response
- The response passes back through each middleware (in reverse order)
- Your application receives the final response
The HttpMiddleware Interface
All middleware components must implement theHttpMiddleware interface:
handle method takes two parameters:
$request: The HTTP request to process$next: The next handler in the middleware chain
- Modify the request before passing it to the next handler
- Short-circuit the chain by returning a response without calling the next handler
- Process the response from the next handler before returning it
- Wrap the response in a decorator for further processing (especially useful for streaming responses)
The BaseMiddleware Abstract Class
While you can implement theHttpMiddleware interface directly, the library provides a convenient BaseMiddleware abstract class that makes it easier to create middleware:
BaseMiddleware, you only need to override the methods relevant to your middleware’s functionality, making the code more focused and maintainable.
Middleware Stack
TheMiddlewareStack class manages the collection of middleware components. It provides methods to add, remove, and arrange middleware in the stack.
Adding Middleware
There are several ways to add middleware to the stack:Removing Middleware
You can remove middleware from the stack by name:Replacing Middleware
You can replace a middleware with another one:Clearing Middleware
You can remove all middleware from the stack:Checking Middleware
You can check if a middleware exists in the stack:Getting Middleware
You can get a middleware from the stack by name or index:Middleware Order
The order of middleware in the stack is important because:- Requests pass through middleware in the order they were added to the stack
- Responses pass through middleware in reverse order
- Authentication middleware
- Logging middleware
- Retry middleware
- Request: Authentication → Logging → Retry → HTTP Driver
- Response: Retry → Logging → Authentication → Your Application
Middleware Application Example
Here’s an example of how middleware is applied in a request-response cycle:Built-in Middleware
The Instructor HTTP client API includes several built-in middleware components for common tasks:Debug Middleware
TheDebugMiddleware logs detailed information about HTTP requests and responses:
- Request URLs
- Request headers
- Request bodies
- Response headers
- Response bodies
- Streaming response data
config/debug.php file:
BufferResponse Middleware
TheBufferResponseMiddleware stores response bodies and streaming chunks for reuse:
StreamByLine Middleware
TheStreamByLineMiddleware processes streaming responses line by line: