GEMVC Request Lifecycle

20 minutes BEGINNER

Understand how HTTP requests flow through GEMVC framework. Learn about server adapters, unified request objects, and how your application code works identically on all webservers.

What You'll Learn

Server-Agnostic Architecture

How GEMVC works across different webservers

Request Flow

Step-by-step request processing

Server Adapters

ApacheRequest and SwooleRequest adapters

Video Coming Soon...

Overview

GEMVC's HTTP layer is designed to be completely server-agnostic. The same application code works identically on Apache, OpenSwoole, and Nginx without any changes.

Key Principle

Your app/ code never changes when switching webservers! The framework handles all platform differences automatically.

This is achieved through:

  • Server Adapters - Convert webserver-specific requests to unified format
  • Unified Request Object - Single interface for all webservers
  • Response Abstraction - Consistent response handling across platforms

Server-Agnostic Architecture

GEMVC uses an adapter pattern to abstract webserver differences:

Webserver-Specific Request
Server Adapter (ApacheRequest | SwooleRequest)
Unified Request Object
Application Code (app/api/, app/controller/)
Unified Response
Webserver-Specific Output

Server Adapters

  • • ApacheRequest
  • • SwooleRequest
  • • NginxRequest (coming soon)

Unified Request

  • • Single interface
  • • Already sanitized
  • • Type-safe access

Response Abstraction

  • • JsonResponse
  • • show() for Apache
  • • showSwoole() for OpenSwoole

Apache/Nginx Request Lifecycle

Here's how requests flow through Apache/Nginx:

1. HTTP Request arrives at Apache/Nginx
2. PHP-FPM processes request
3. index.php loads Bootstrap
4. ApacheRequest adapter created
  • • Sanitizes all headers
  • • Sanitizes GET, POST, PUT, PATCH
  • • Extracts files, cookies, auth headers
  • • Creates unified Request object
5. Bootstrap routes to API service
6. API service validates schema
7. Controller handles business logic
8. Model performs data operations
9. JsonResponse returned
10. JsonResponse->show() outputs to Apache

OpenSwoole Request Lifecycle

Here's how requests flow through OpenSwoole:

1. HTTP Request arrives at OpenSwoole server
2. OpenSwooleServer receives request
3. SecurityManager checks path access
4. SwooleRequest adapter created
  • • Sanitizes headers
  • • Sanitizes request data
  • • Extracts files, cookies, auth headers
  • • Creates unified Request object
5. SwooleBootstrap routes to API service
6. API service validates schema
7. Controller handles business logic
8. Model performs data operations
9. JsonResponse returned
10. JsonResponse->showSwoole() outputs to OpenSwoole

Server Adapters

Server adapters convert webserver-specific requests to a unified format. They handle all sanitization and normalization automatically.

ApacheRequest Adapter

Purpose:

Converts Apache/PHP-FPM requests to unified Request object.

Key Features:

  • ✓ Sanitizes all HTTP headers ($_SERVER['HTTP_*'])
  • ✓ Sanitizes GET, POST, PUT, PATCH data
  • ✓ Handles file uploads ($_FILES)
  • ✓ Extracts cookies and auth headers
  • ✓ Creates unified Request object

SwooleRequest Adapter

Purpose:

Converts OpenSwoole requests to unified Request object.

Key Features:

  • ✓ Sanitizes OpenSwoole request headers
  • ✓ Handles raw request body parsing
  • ✓ Normalizes file uploads
  • ✓ Filters dangerous cookies
  • ✓ Creates unified Request object

Unified Request Object

The Request class provides a unified interface for all webservers. All data is already sanitized by the adapter.

Key Features:

  • Already Sanitized - All inputs sanitized by adapter
  • Type-Safe Access - Methods like intValueGet(), stringValueGet()
  • Schema Validation - definePostSchema(), defineGetSchema()
  • JWT Authentication - Built-in JWT token handling
  • Query Helpers - Filtering, sorting, pagination

Request Properties:

  • $request->post - POST data (sanitized)
  • $request->get - GET data (sanitized)
  • $request->put - PUT data (sanitized)
  • $request->files - File uploads
  • $request->auth() - JWT authentication

Response Handling

GEMVC abstracts responses to work across all webservers. Your application code is identical, and the framework handles the output differences.

Apache/Nginx

Uses JsonResponse->show():

  • • Sets headers
  • • Outputs JSON
  • • Calls die() to terminate

OpenSwoole

Uses JsonResponse->showSwoole():

  • • Sets headers
  • • Outputs JSON
  • • Returns (no die - persistent process)

Your application code is identical! The framework automatically calls the correct method (show() or showSwoole()) based on the webserver.

Automatic Security

All server adapters automatically sanitize all inputs before your application code receives them:

HTTP Headers

All HTTP headers are sanitized automatically by the adapter.

Request Data

GET, POST, PUT, PATCH data is sanitized automatically (XSS prevention).

Cookies

Dangerous cookies are filtered automatically.

File Names

Uploaded file names are sanitized automatically.

Result: Your application code receives already-sanitized data! No need to manually sanitize inputs.

Key Benefits

Server-Agnostic Code

Your app/ code works identically on Apache, OpenSwoole, and Nginx without any changes.

Automatic Sanitization

All inputs are sanitized automatically by adapters before reaching your code.

Unified Interface

Same Request interface for all webservers - no platform-specific code needed.

Consistent Responses

Framework handles response output differences automatically.

🎉 Request Lifecycle Complete!

Now you understand how requests flow through GEMVC! Let's start building your first API.