TrishulApi\Core\Http\Request

Namespace: TrishulApi\Core\Http
Author: Shyam Dubey
Since: v1.0.0

Overview

The Request class is the central point for handling HTTP requests in the Trishul API framework. It provides convenient access to query parameters, headers, request body, cookies, sessions, and path variables. This class is designed to be used in controllers, middleware, and other core components to interact with incoming HTTP requests.

Usage Example


// Example usage in a controller or middleware

use TrishulApi\Core\Http\Request;

function handleRequest(Request $request) {
    // Get query parameters
    $params = $request->query_params()->all();

    // Get a specific header
    $authHeader = $request->header()->get('Authorization');

    // Get request body (for POST/PUT)
    $body = $request->body()->asArray();

    // Get session data
    $userId = $request->session()->get('user_id');

    // Get cookies
    $cookieValue = $request->cookie()->get('session_token');

    // Get path variables
    $id = $request->path()->get('id');

    // Get request URL
    $url = $request->get_url();

    // Get client IP
    $ip = $request->get_ip();
}
        

Constructor

__construct(string $url)

Initializes the request object with the given URL. Internally sets up query parameters, headers, body, session, cookies, and path variables.

ParameterTypeDescription
$urlstringThe URL on which the request is coming.

Methods

body(): RequestBody

Returns the RequestBody object representing the request payload (useful for POST/PUT requests).

query_params(): QueryParams

Returns the QueryParams object for accessing query parameters.

update(Request $request): void

Updates the current request object with another request's data. Useful in middleware when modifying the request.

set_body(RequestBody $body): void

Sets the request body. Throws NullPointerException if the current body is null.

get_url(): string

Returns the URL of the current request.

get_ip(): string|null

Returns the client's IP address.

header(): Header

Returns the Header object for accessing HTTP headers.

set_header(Header $header): void

Sets the request headers (used internally).

session(): Session

Returns the Session object for managing session data.

set_session(Session $session): void

Sets the session object (used internally).

cookie(): Cookie

Returns the Cookie object for accessing cookies.

set_cookie(Cookie $cookie): void

Sets the cookie object (used internally).

set_path($data): void

Sets the path variables (used internally).

path(): PathVariable

Returns the PathVariable object for accessing path variables.

Notes

Related Classes

Changelog