Request

The Request class acts as the central nervous system for HTTP data. It normalizes inputs from $_GET, $_POST, and JSON bodies into a unified API, while providing utilities for state management and smart redirection.

Data Containers

Parameters

Unified access to input data.

  • static ArrayObject $params Merged data from GET, POST, or JSON body.
  • static mixed $data Raw parsed body data (e.g., decoded JSON array).
  • static ArrayObject $dynamicParams Captured route parameters (e.g., from [id], [...other]).

Client State

Persistence and context.

  • static ArrayObject $localStorage Server-side access to the client's appState. Allows reading/writing state that syncs with the browser.

Request Flags & Context

Method Flags

  • $isGet
  • $isPost
  • $isPut
  • $isDelete
  • $isPatch
  • $isHead
  • $isOptions

Request Type

  • $isAjax - XHR/Fetch request
  • $isWire - PulsePoint Wire request
  • $isXFileRequest - File fetch context

Context Info

  • $method - (string) GET, POST...
  • $protocol - http:// or https://
  • $remoteAddr - IP Address
  • $requestedWith - Header value

URL & Path Properties

Property Description
$pathname The request path (e.g., /dashboard/user).
$uri Full URI including query strings.
$documentUrl Full URL including protocol and domain.
$referer The HTTP referer URL.

Utility Methods

static redirect(string $url, bool $replace = true, int $code = 0): void

Smart redirection. If the request is Wire or Ajax, it returns a control string redirect_7F834=... instead of a header, allowing the frontend framework to handle the navigation without a hard reload.

static getBearerToken(): ?string

Parses the Authorization header and extracts the Bearer token. Returns null if missing.

static handlePreflight(): void

Checks if the method is OPTIONS. If so, sends 200 OK and exits immediately. Useful for API endpoints.

static getDecodedUrl(string $uri): string

Parses a URI and returns the path with the query string decoded.

Usage Examples

1. Basic Initialization & Param Access

<?php
use PP\Request;

// Accessing params (works for GET, POST, or JSON body)
$userId = Request::$params->userId ?? null;

if (Request::$isPost) {
    // Handle form submission
}
?>

2. Using Local Storage State

This feature allows the PHP backend to read state that was set in the browser's localStorage (under key appState_59E13).

<?php
use PP\Request;

// Read preference set by JS on the client
$darkMode = Request::$localStorage->theme === 'dark';

// If we modify this array object, the framework can optionally 
// sync it back to the client response in specific setups.
?>

<div class="<?= $darkMode ? 'bg-black text-white' : 'bg-white text-black' ?>">
    Content
</div>

3. API Endpoint Protection

<?php
use PP\Request;
use PP\Headers\Boom;

Request::handlePreflight(); // Handle CORS OPTIONS check

// Ensure only POST is allowed
if (!Request::$isPost) {
    Boom::methodNotAllowed()->toResponse();
}

$token = Request::getBearerToken();
if (!$token) {
    Boom::unauthorized()->toResponse();
}

// Process JSON Data
$data = Request::$data;
?>