Comprehensive HTTP handling including request/response objects, routing, middleware, and kernel processing. These classes provide the foundation for handling web requests and generating responses in your Forge Engine applications.
Represents an HTTP request and provides methods to access request data, headers, and other request-related information. This is the primary way to interact with incoming HTTP requests in your application.
Create a new Request instance with the given parameters.
// Create from globals
$request = Request::createFromGlobals();
// Create manually
$request = new Request(
    $_GET,
    $_POST,
    [],
    $_COOKIE,
    $_FILES,
    $_SERVER,
    file_get_contents('php://input')
);Get a value from the query string (GET parameters).
$page = $request->get('page', 1);
$search = $request->get('search');Get a value from the POST parameters.
$username = $request->post('username');
$email = $request->post('email', 'default@example.com');Get a value from any input source (GET, POST, JSON).
$data = $request->input('user.name');
$settings = $request->input('settings', []);Get all input data as an array.
$allData = $request->all();
$filtered = $request->only(['name', 'email']);
$excluded = $request->except(['password', 'token']);Get the HTTP request method (GET, POST, PUT, DELETE, etc.).
if ($request->method() === 'POST') {
    // Handle POST request
}Check if the request method matches the given method.
if ($request->isMethod('POST')) {
    // Handle POST request
}Get the full URL of the request.
$currentUrl = $request->url();
$fullUrl = $request->fullUrl(); // Includes query stringGet the request path without the domain.
$path = $request->path(); // e.g., /users/123Get a header value by key.
$contentType = $request->header('Content-Type');
$authToken = $request->header('Authorization');Get the bearer token from the Authorization header.
$token = $request->bearerToken();
if ($token) {
    // Validate JWT token
}Check if the request accepts the given content type(s).
if ($request->accepts('application/json')) {
    return response()->json($data);
}Represents an HTTP response and provides methods to set response data, headers, and status codes. This class is used to construct and send HTTP responses back to the client.
Create a new Response instance.
// Basic response
$response = new Response('Hello World', 200, ['Content-Type' => 'text/plain']);
// JSON response
$response = new Response(json_encode($data), 200, ['Content-Type' => 'application/json']);Set the response content.
$response->setContent('Hello World');
$response->setContent(json_encode($data));Get the current response content.
$content = $response->getContent();Set the HTTP status code.
$response->setStatusCode(200); // OK
$response->setStatusCode(404); // Not Found
$response->setStatusCode(500); // Internal Server ErrorGet the current status code.
$statusCode = $response->getStatusCode();Set a response header.
$response->header('Content-Type', 'application/json');
$response->header('Cache-Control', 'no-cache');
$response->header('X-Custom-Header', 'value');Set multiple headers at once.
$response->headers([
    'Content-Type' => 'application/json',
    'Cache-Control' => 'no-cache',
    'X-Rate-Limit' => '100'
]);Get all response headers.
$headers = $response->getHeaders();
foreach ($headers as $name => $value) {
    echo "$name: $value\n";
}Create a JSON response.
return response()->json(['message' => 'Success'], 200);
return response()->json($user, 201);Create a redirect response.
return response()->redirect('/dashboard');
return response()->redirect('/login', 301);Create a file download response.
return response()->download('/path/to/file.pdf');
return response()->download('/path/to/file.pdf', 'custom-name.pdf');Handles HTTP request processing through middleware pipeline. The Kernel is responsible for processing incoming requests through the middleware stack and generating appropriate responses.
Process a request through the middleware pipeline.
$kernel = new Kernel();
$response = $kernel->handle($request);
$response->send();Add middleware to the pipeline.
$kernel->pushMiddleware(AuthenticationMiddleware::class);
$kernel->pushMiddleware(CorsMiddleware::class);
$kernel->pushMiddleware(RateLimitMiddleware::class);Get all registered middleware.
$middleware = $kernel->getMiddleware();
foreach ($middleware as $class) {
    echo "Registered: $class\n";
}Interface for creating HTTP middleware. Middleware provides a convenient mechanism for filtering HTTP requests entering your application and processing responses before they're sent to the client.
Process an incoming request and return a response.
class AuthenticationMiddleware implements Middleware
{
    public function process(Request $request, callable $next): Response
    {
        // Before request handling
        if (!$this->authenticate($request)) {
            return new Response('Unauthorized', 401);
        }
        
        // Pass to next middleware
        $response = $next($request);
        
        // After request handling
        $response->header('X-Authenticated', 'true');
        
        return $response;
    }
}class CorsMiddleware implements Middleware
{
    public function process(Request $request, callable $next): Response
    {
        $response = $next($request);
        
        $response->headers([
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
            'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
        ]);
        
        return $response;
    }
}class RateLimitMiddleware implements Middleware
{
    private $maxRequests = 100;
    private $window = 3600; // 1 hour
    
    public function process(Request $request, callable $next): Response
    {
        $key = 'rate_limit:' . $request->ip();
        $requests = $this->cache->get($key, 0);
        
        if ($requests >= $this->maxRequests) {
            return new Response('Rate limit exceeded', 429);
        }
        
        $this->cache->increment($key);
        $this->cache->expire($key, $this->window);
        
        return $next($request);
    }
}Common patterns and examples for working with the HTTP Layer classes.
// Get current request
$request = Request::createFromGlobals();
// Access request data
$username = $request->post('username');
$email = $request->post('email');
$page = $request->get('page', 1);
// Check request method
if ($request->isMethod('POST')) {
    // Handle form submission
    $data = $request->all();
    
    // Validate input
    $validator = new Validator($data, [
        'username' => 'required|min:3',
        'email' => 'required|email'
    ]);
    
    if ($validator->fails()) {
        return response()->json(['errors' => $validator->errors()], 422);
    }
    
    // Process data
    $user = User::create($data);
    
    return response()->json(['user' => $user], 201);
}// Controller method
public function getUser(Request $request, $id)
{
    // Check if request wants JSON
    if (!$request->accepts('application/json')) {
        return response()->setContent('Unsupported format'), 406);
    }
    
    // Get user with authentication check
    $user = User::find($id);
    
    if (!$user) {
        return response()->json([
            'error' => 'User not found',
            'code' => 'USER_NOT_FOUND'
        ], 404);
    }
    
    // Check authorization
    if ($user->id !== $request->user()->id) {
        return response()->json([
            'error' => 'Unauthorized',
            'code' => 'UNAUTHORIZED'
        ], 403);
    }
    
    // Return success response
    return response()->json([
        'data' => $user->toArray(),
        'meta' => [
            'timestamp' => now()->toIso8601String(),
            'version' => '1.0'
        ]
    ], 200);
}public function uploadFile(Request $request)
{
    // Validate file upload
    if (!$request->hasFile('document')) {
        return response()->json(['error' => 'No file uploaded'], 400);
    }
    
    $file = $request->file('document');
    
    // Validate file
    $validator = Validator::make(['file' => $file], [
        'file' => 'required|file|mimes:pdf,doc,docx|max:10240'
    ]);
    
    if ($validator->fails()) {
        return response()->json(['errors' => $validator->errors()], 422);
    }
    
    // Store file
    $path = $file->store('documents', 'local');
    
    // Create file record
    $document = Document::create([
        'name' => $file->getClientOriginalName(),
        'path' => $path,
        'size' => $file->getSize(),
        'mime_type' => $file->getMimeType()
    ]);
    
    return response()->json([
        'message' => 'File uploaded successfully',
        'document' => $document
    ], 201);
}Recommended patterns and guidelines for working with the HTTP Layer.