Namespace: TrishulApi\Core\Middleware
Author: Shyam Dubey
Since: v1.0.0
The MiddlewareInterface
defines the contract for creating custom middleware in the Trishul API framework. Middleware allows you to intercept and manipulate HTTP requests and responses before and after the main application logic executes. This is useful for tasks such as authentication, logging, input validation, and modifying responses.
To create a custom middleware, implement the MiddlewareInterface
and define the required methods:
use TrishulApi\Core\Middleware\MiddlewareInterface;
use TrishulApi\Core\Http\Request;
use TrishulApi\Core\Http\Response;
class AuthMiddleware implements MiddlewareInterface
{
public function handle_request(Request $request): Request
{
// Example: Check authentication
if (!$request->session()->get('user_id')) {
// You can modify the request or throw an exception
}
return $request;
}
public function handle_response(Response $response): Response
{
// Example: Add a custom header to the response
$response->get_header()->set('X-Processed-By', 'AuthMiddleware');
return $response;
}
}
handle_request(Request $request): Request
Intercepts the incoming HTTP request before it reaches the main application logic. You can inspect, modify, or validate the request here.
Parameter | Type | Description |
---|---|---|
$request | Request | The incoming HTTP request object. |
Returns: Request
(possibly modified)
handle_response(Response $response): Response
Intercepts the outgoing HTTP response before it is sent to the client. You can modify headers, cookies, or the response body here.
Parameter | Type | Description |
---|---|---|
$response | Response | The outgoing HTTP response object. |
Returns: Response
(possibly modified)
Request
or Response
object from your methods.TrishulApi\Core\Http\Request
TrishulApi\Core\Http\Response