Request

The Request class provides a structured way to manage HTTP request data in your application. It includes various utilities for processing parameters, methods, headers, and dynamic properties.

Purpose

The Request class acts as a central hub for accessing and managing all request-related data, such as parameters, headers, and methods. It simplifies the process of handling different request types, including AJAX and preflight requests, while providing helpful tools like URL generation and redirection.

Static Properties

The following static properties are available in the Request class:

  • static \ArrayObject $params: Holds request parameters (e.g., $_GET or JSON body).
  • static \ArrayObject $dynamicParams: Holds dynamic parameters passed during the request.
  • static \ArrayObject $localStorage: Manages local storage data, interacting with the browser's local storage key appState_59E13. When updated, it can optionally trigger a request to the server.
  • static string $method: The request method (e.g., GET, POST).
  • static bool $isAjax: Indicates if the request is an AJAX request.
  • static bool $isWire: Indicates if the request is a wire request.
  • static mixed $data: Parsed request data (e.g., JSON body).
  • static string $protocol: The request protocol (e.g., http or https).
  • static string $pathname: The request pathname (e.g., /home).
  • static string $uri: The request URI (e.g., home?param=value).
  • const baseUrl: The base URL of the application, typically pointing to /src/app.
  • static string $referer: The request referer URL.
  • static string $contentType: The request content type.
  • static string $protocol: The request protocol (e.g., http or https).
  • static string $domainName: The request domain name (e.g., example.com).
  • static string $scriptName: The request script name (e.g., index.php).
  • static string $documentUrl: The request document URL.
  • static string $fileToInclude: The file to include based on the request.
  • static string $isGet: Indicates if the request method is GET.
  • static string $isPost: Indicates if the request method is POST.
  • static string $isPut: Indicates if the request method is PUT.
  • static string $isDelete: Indicates if the request method is DELETE.
  • static string $isPatch: Indicates if the request method is PATCH.
  • static string $isHead: Indicates if the request method is HEAD.
  • static string $isOptions: Indicates if the request method is OPTIONS.
  • static string $isXFileRequest: Indicates if the request is an X-File request.
  • static string $requestedWith: The request X-Requested-With header value.

Methods

Below are the key methods provided by the Request class:

  • static void handlePreflight(): Handles preflight OPTIONS requests.
  • static void checkAllowedMethods(): Ensures that the request method is allowed.
  • static string getBearerToken(): Retrieves the Bearer token from the authorization header.
  • static void redirect(string $url): Redirects the client to a specified URL.

For more detailed information about the implementation, please refer to the Request class in the src/Lib/Request.php file.

Usage Examples

Using $localStorage

The $localStorage property allows you to store and retrieve data in the browser's local storage using the key appState_59E13. This can be useful for persisting user preferences, theme settings, or other data across sessions. Changes made on the client side can be synchronized with the server, ensuring that the state is maintained consistently between requests.

<?php

use Lib\Request;

$sidebarOpen = Request::$localStorage->sidebarOpen ?? false;

?>

<div class="w-screen h-screen grid place-items-center">
    <div class="flex flex-col gap-2">
        <span>sidebarOpen: <?= $sidebarOpen ?></span>
        <button onclick="toggleSidebar" class="p-2 bg-blue-500 text-white rounded">
            Toggle Sidebar
        </button>
    </div>
</div>

<script>
    function toggleSidebar() {
        store.setState({
            sidebarOpen: !store.state.sidebarOpen
        }, true); // Optionally sync with server on change. 
              // If set to false or omitted, it will only update the local storage.
    }
</script>

Using baseUrl

The benefit of using baseUrl is that it allows you to easily reference assets and resources in your application without hardcoding the path each time. By setting the base URL to /src/app, you can access assets and resources easily. However, if needed, you can still specify the full path to the assets and resources manually, like this: /src/app/assets/images/logo.png.

<?php
  use Lib\Request;

  ?>

  <script src="<?= Request::baseUrl; ?>/js/index.js"></script>
  <img src="<?= Request::baseUrl; ?>/assets/images/check.svg" alt="Checkmark">

1. Initializing the Request

<?php
  use Lib\Request;

  $id = Request::$params->id ?? 'No ID provided';
  echo "ID: $id";
  // OR
  $params = Request::$params;
  echo $params->id ?? 'No ID provided';
  ?>

2. Handling Dynamic Parameters

This feature is particularly useful when dealing with requests that include unknown or dynamic parameters, such as post/[id]/index.php or post/[...dynamic]/index.php.

<?php
  use Lib\Request;

  print_r(Request::$dynamicParams);
  ?>

3. Handling JSON Requests

<?php
  use Lib\Request;

  if (Request::$data) {
      echo json_encode(Request::$data);
  } else {
      echo 'No JSON data provided.';
  }
  ?>

4. Redirecting Requests

<?php
  use Lib\Request;

  Request::redirect('/home');
  ?>

Use the redirect method to navigate users dynamically. For AJAX or wire requests, the method outputs a custom redirect response.