Middleware
Middleware is a queue of callbacks that will be executed in order. The middleware stack is executed in the order that the middleware is added to the stack. The middleware stack is executed for each request. The middleware is responsible for handling the request and generating the response.
Middleware is a good place to add logging, authentication, routing, etc.
Swift middleware is compliant with the PSR-15 middleware interface. Any implementation of the Psr\Http\Server\MiddlewareInterface
can be used and will automatically be recognized by the kernel and added to the queue.
#
UsageMiddlewares will automatically be added to the kernel. All middleware must implement the Psr\Http\Server\MiddlewareInterface
or Swift\Kernel\Middleware\MiddlewareInterface
which is an extension of the PSR interface.
#
Modifying the responseThe handler returns a response. The response can be modified by the middleware.
#
Async middlewareReactPHP can be used to handle async middleware. This is a good way to handle independent tasks, so they won't block the request.
#
Middleware queueAll middleware is added to a queue in which it will be executed in order when it's added to the stack. This can be influenced by implementing the Swift\Middleware\MiddlewareInterface
, which is an extension of the PSR interface.
#
Middleware execution orderMiddleware is executed in the order that it is added to the stack. This order can be influenced by implementing the Swift\Middleware\MiddlewareInterface
, which is an extension of the PSR interface. This interface also adds the getOrder(): int
method. All middlewares not implementing this method will default to order 0.
#
Default middlewareSwift itself relies fully on middleware. The middlewares that are available by default in the following order are:
#
RequestParsingMiddlewareThe Swift\HttpFoundation\Kernel\Middleware\RequestParsingMiddleware
parses any PSR-7 request to a Swift Swift\HttpFoundation\ServerRequest
request. It has order - 80.
#
DefaultTimeZoneMiddlewareThe Swift\Kernel\Middleware\DefaultTimeZoneMiddleware
sets the default timezone as configured in the application config. It has order - 50.
#
DeprecationMiddlewareThe Swift\Kernel\Middleware\DeprecationMiddleware
sets the correct Deprecation level for the application. It has order -50.
#
CorsMiddlewareThe Swift\Kernel\Middleware\CorsMiddleware
intercepts any CORS preflight request and returns a CORS response. It has order - 45.
#
AuthMiddlewareThe Swift\Security\Authentication\Kernel\Middleware\AuthMiddleware
is responsible for handling authentication. It has order - 10.
#
RateLimitingMiddlewareThe Swift\Security\RateLimiter\Kernel\Middleware\RateLimitingMiddleware
is responsible for default rate limiting. It has order - 5.
#
RequestLoggingMiddlewareThe Swift\Kernel\Middleware\RequestLoggingMiddleware
logs all requests to the database if enabled. It has order 0.
#
GraphQlMiddlewareThe Swift\GraphQl\Middleware\RequestMiddleware
is responsible for handling GraphQl requests. It has order 30.
#
RestMiddlewareThe Swift\Router\Kernel\Middleware\RestMiddleware
is responsible for handling REST requests. It has order 40.