Skip to main content

Introduction

Middleware provides a convenient mechanism to filter and process HTTP requests entering your application. Lyger’s middleware system is inspired by Laravel and allows you to chain multiple middleware handlers together.

Middleware Concept

Middleware acts as a bridge between a request and a response. Each middleware can:
  • Inspect the incoming request
  • Modify the request
  • Pass control to the next middleware
  • Return a response early (short-circuit)
  • Modify the response after the handler executes

Creating Middleware

All middleware must extend the Middleware base class and implement the handle method:
The $next callable represents the next middleware in the chain or the final route handler.

Built-in Middleware

Lyger includes several built-in middleware classes for common use cases:

CORS Middleware

Handle Cross-Origin Resource Sharing (CORS) headers:
Configuration Options:
  • allowed_origins: Array of allowed origins or ['*'] for all
  • allowed_methods: HTTP methods to allow
  • allowed_headers: Request headers to allow
  • exposed_headers: Response headers to expose
  • max_age: Preflight cache duration in seconds
  • supports_credentials: Whether to support credentials

Rate Limiting Middleware

Prevent abuse by limiting the number of requests:
The rate limiter automatically adds headers to responses:
  • X-RateLimit-Limit: Maximum attempts allowed
  • X-RateLimit-Remaining: Remaining attempts
  • X-RateLimit-Reset: Unix timestamp when the limit resets
  • Retry-After: Seconds until retry (when limit exceeded)
When the limit is exceeded, it returns a 429 response:
The rate limiter uses IP address and URI as the signature for tracking requests.

Authentication Middleware

Simple token-based authentication:
The middleware expects the token in the Authorization header (or custom header):
Unauthorized responses return 401:

JSON Parser Middleware

Automatically parse JSON request bodies:
This middleware:
  • Detects application/json content type
  • Parses the JSON body
  • Makes data available via $request->input()
Without this middleware, JSON request bodies won’t be automatically parsed.

Logging Middleware

Log all HTTP requests and responses:
Default output format:

Chaining Middleware

Middleware can be chained together using the setNext() method:
The request flows through the middleware chain:

Creating Custom Middleware

Here are some practical examples of custom middleware:

Short-Circuiting

Middleware can return a response early without calling the next middleware:

Conditional Middleware

Apply middleware based on conditions:

Middleware Best Practices

Keep middleware focused: Each middleware should handle one specific concern (authentication, logging, etc.)
Order matters: Chain middleware in logical order. For example, rate limiting should come before authentication to protect auth endpoints.
Performance: Avoid heavy operations in middleware that runs on every request. Consider caching when possible.
Always call $next($request) unless you intentionally want to short-circuit the request. Forgetting this will break your application.

Common Middleware Stack

A typical production middleware stack might look like:

Next Steps

Basic Routing

Learn about defining routes

Route Parameters

Capture dynamic URL segments