VOOZH about

URL: https://deepwiki.com/hypervel/foundation/4-http-request-handling

⇱ HTTP Request Handling | hypervel/foundation | DeepWiki


Loading...
Last indexed: 7 February 2026 (101eff)
Menu

HTTP Request Handling

Purpose and Scope

This document provides an overview of the HTTP layer in the Hypervel Foundation framework, covering the request lifecycle from incoming HTTP requests to final responses. It describes the core components involved in processing HTTP requests: the HTTP Kernel, middleware system, form request validation, and exception handling.

For detailed information about specific subsystems, see:

Overview

The HTTP layer sits between the Swoole server and the application's controllers. When a request arrives, it flows through several processing stages: initialization, middleware execution, routing/dispatch, controller execution, exception handling (if needed), and response emission.

The framework extends Hyperf's HTTP server infrastructure while adding Laravel-like request handling patterns including form requests, validation, authorization, and a rich exception handling system.

Core Components

The HTTP layer consists of four primary components that work together to process requests:


Sources: src/Http/Kernel.php src/Exceptions/Handler.php src/Exceptions/Contracts/ExceptionHandler.php

Component Responsibilities

ComponentClassPrimary Responsibilities
HTTP KernelHypervel\Foundation\Http\KernelRequest initialization, middleware coordination, exception handling setup, uploaded file conversion
Exception HandlerHypervel\Foundation\Exceptions\HandlerException reporting, rendering to responses, context building, error view resolution
Middleware SystemHypervel\Foundation\Http\Traits\HasMiddlewareMiddleware registration, resolution, priority ordering, group management
Form RequestHypervel\Validation\FormRequestInput validation, authorization checks, input casting, validation error responses

Sources: src/Http/Kernel.php1-166 src/Exceptions/Handler.php47-878

Request Lifecycle

The following sequence shows how an HTTP request flows through the framework from Swoole server to final response:


Sources: src/Http/Kernel.php49-165 src/Exceptions/Handler.php264-450 src/Providers/FormRequestServiceProvider.php16-22

Request Processing Stages

1. Request Initialization

The Kernel::onRequest() method receives the raw Swoole request and response objects, then performs initialization:

  • Waits for worker process to start via CoordinatorManager::until(Constants::WORKER_START)->yield()
  • Initializes PSR-7 Request and Response objects
  • Trims trailing slashes from URI paths (except for root /)
  • Converts Hyperf UploadedFile instances to Hypervel UploadedFile instances with additional capabilities

Sources: src/Http/Kernel.php49-76

2. Middleware Execution

The kernel resolves and executes middleware in priority order:

  1. Global middleware (applied to all routes)
  2. Route-specific middleware (defined on individual routes)
  3. Middleware groups (collections of middleware)

See Middleware System for details on registration and execution.

Sources: src/Http/Kernel.php78-82

3. Routing and Dispatch

The Hyperf dispatcher routes the request to the appropriate controller method. If the controller method has FormRequest parameters, they are automatically resolved and validated before the controller executes.

Sources: src/Providers/FormRequestServiceProvider.php18-21

4. Exception Handling

If any exception occurs during processing, the Handler class processes it:

  1. Mapping: Exception may be mapped to a different type via registered mappers
  2. Reporting: Exception is logged unless in the $dontReport list
  3. Rendering: Exception is converted to an HTTP response (HTML or JSON)

Sources: src/Exceptions/Handler.php264-450

5. Response Emission

The final response is emitted to the client via Hyperf's response emitter. Lifecycle events are dispatched:

  • RequestHandled - Dispatched immediately after response is ready
  • RequestTerminated - Deferred event dispatched after response sent

Sources: src/Http/Kernel.php86-155

Exception Types and Handling

The exception handler provides special handling for common exception types:


Sources: src/Exceptions/Handler.php110-679

Exception Response Logic

Exception TypeReported?Default JSON BehaviorDefault HTML Behavior
ValidationExceptionNoJSON with errors arrayRedirect back with errors flashed to session
AuthenticationExceptionNo401 JSON responseRedirect to login route
AuthorizationExceptionNo403 or custom status403 error view
ModelNotFoundExceptionNo404 JSON response404 error view
TokenMismatchExceptionNo419 JSON response419 error view
HttpExceptionNoStatus code with messageError view (e.g., errors::404)
Other exceptionsYes500 with details (if debug)Debug page or generic 500 view

Sources: src/Exceptions/Handler.php110-779

Uploaded File Conversion

The kernel automatically converts Hyperf's UploadedFile instances to Hypervel's enhanced UploadedFile class, which provides additional methods compatible with Laravel's API:


The conversion is recursive, handling nested arrays of uploaded files properly.

Sources: src/Http/Kernel.php64-121

Configuration

The HTTP layer is configured through the application configuration file and environment variables:

Configuration KeyPurposeDefault
app.debugEnable detailed error pagesfalse
app.envApplication environment'production'
app.urlBase URL for the application'http://localhost'
app.timezoneDefault timezone'UTC'
app.localeDefault locale'en'

Sources: publish/app.php9-171

Debug Mode Behavior

When app.debug is true:

  • Exceptions render with full stack traces and context
  • JSON responses include exception class, file, line, and trace
  • Detailed error pages are shown instead of generic error views
  • Validation errors include more debugging information

When app.debug is false:

  • Exceptions render generic error messages
  • JSON responses contain only simple error messages
  • Custom error views are used (from resources/views/errors/)
  • Sensitive information is hidden from responses

Sources: src/Exceptions/Handler.php671-836

Integration Points

The HTTP layer integrates with other framework subsystems:

  • Application Container (Application Container and Dependency Injection): Used for dependency injection in controllers, middleware, and exception handlers
  • Service Providers (Service Providers and Application Lifecycle): FormRequestServiceProvider registers form request validation hooks
  • Routing System: Resolves routes to controllers and determines middleware
  • Validation System: Validates form requests before controller execution
  • Session Management: Flashes validation errors to session for redirect responses
  • Authentication System: Handles authentication exceptions and guards
  • Event System: Dispatches RequestReceived, RequestHandled, and RequestTerminated events

Sources: src/Exceptions/Handler.php136-141 src/Providers/FormRequestServiceProvider.php1-23 src/Http/Kernel.php123-155