boubacamara/ganega-http

Provides simple and intuitive classes for handling HTTP requests and responses in PHP applications. The Request class encapsulates HTTP request data, making it easy to manage and access parameters, body, session, cookies, and server information. The Response class simplifies the creation and managem

Installs: 9

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/boubacamara/ganega-http

1.0.0 2024-06-23 15:16 UTC

This package is auto-updated.

Last update: 2025-12-23 21:44:52 UTC


README

The Request and Response classes provide simple and intuitive interfaces for handling HTTP requests and responses in your PHP application. They encapsulate request data and HTTP responses, making their manipulation and management easier.

Purpose

The Request and Response classes are designed to simplify interaction with HTTP request and response data. They offer methods to check and retrieve request parameters, request body data, sessions, cookies, and server information, as well as to configure and send HTTP responses. This abstraction allows you to manipulate HTTP requests and responses cleanly and efficiently without directly accessing global variables like $_GET, $_POST, $_SESSION, $_COOKIE, $_SERVER, and $_FILES.

Ease of Use

The Request and Response classes provide a clear and concise interface with well-defined methods for common HTTP request and response operations. Using these classes helps reduce repetitive code and makes the code more readable and maintainable.

Installation

To use these classes in your project, simply include them in your PSR-4 autoloader or directly import them into your PHP file.

composer require boubacamara/ganega-http

Usage Examples

Request Class

Create a Request instance from global variables

use Ganega\Http\Request\Request;

$request = Request::fromGlobals();
Check if the request has a body
if ($request->hasBody()) {
    // Process the request body
}
Get the request body
$body = $request->getBody();
Check and retrieve a request parameter
if ($request->hasInput('username')) {
    $username = $request->getInput('username');
}
Manipulate sessions
// Add a session
$request->addSession('user_id', 123);

// Retrieve a session
$user_id = $request->getSession('user_id');

// Delete a session
$request->deleteSession('user_id');

// Destroy all sessions
$request->destroySession();
Manipulate cookies
// Add a cookie
$request->addCookie('test_cookie', 'value', ['expires' => time() + 3600]);

// Retrieve a cookie
$cookieValue = $request->getCookie('test_cookie');

// Delete a cookie
$request->destroyCookie('test_cookie');
Retrieve URL and server information
// Get the URL path
$urlPath = $request->getUrlPath();

// Get the full URL
$url = $request->getUrl();

// Check the request method
if ($request->hasMethod('POST')) {
    // Process the POST request
}

// Get the request method
$method = $request->getMethod();

// Get the script name
$scriptName = $request->getScriptName();

// Get the server scheme (HTTP/HTTPS)
$serverScheme = $request->getServerScheme();

// Get the host
$host = $request->getHost();

// Get the server port
$port = $request->getPort();

// Check and get the server referrer
if ($request->hasServerReferer()) {
    $referer = $request->getServerReferer();
}
Parse the request body
$parsedBody = $request->bodyParser();

Response Class

Create a Response instance

use Ganega\Http\Request\Response;

$response = new Response();
Set the response content
$response->setContent('Hello, World!');
Add content to the existing response
$response->addContent(' More content.');
Set an HTTP header
$response->setHeader('Content-Type', 'application/json');
Set the HTTP status code
$response->setStatusCode(404);
Send the response
echo $response->send();