Request class and begin Frontend.

This commit is contained in:
2026-03-20 15:54:29 +01:00
parent 6c068443dc
commit 72108d4d03
18 changed files with 540 additions and 2 deletions

51
src/Http/JSONResponse.php Normal file
View File

@@ -0,0 +1,51 @@
<?php
namespace App\Http;
/**
* Permet de renvoyer une réponse de route au format JSON.
*/
class JSONResponse {
/**
* Les données ajoutés au fichier JSON.
* @var array|mixed
*/
public private(set) array $data;
/**
* Le code HTML de la réponse.
* @var int|mixed
*/
public private(set) int $htmlCode;
public function __construct( $data = [], $code = 200 ){
$this->data = $data;
$this->htmlCode = $code;
$this->returnResponse();
}
public function returnResponse(): never {
header( 'Content-type: application/json' );
http_response_code( $this->htmlCode );
$this->data['_status'] = $this->htmlCode;
$json = json_encode( $this->data );
echo $json;
die();
}
public static function sendSuccess( $data = [] ): self {
$data['success'] = true;
return new self( $data, 200 );
}
public static function sendError( $data = [] ): self {
$data['success'] = false;
return new self( $data, 400 );
}
}

53
src/Http/Request.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
namespace App\Http;
use App\Helpers\SanitizeTrait;
use App\Kernel;
/**
* Classe utilitaire ayant plusieurs méthodes pour gérer la requête actuelle.
*/
class Request {
use SanitizeTrait;
/**
* Bloquer les CORS venant d'autres sites.
* @return void
*/
public static function setCORS(): void {
$siteUrl = Kernel::$configs['general']['website_url'];
header("Access-Control-Allow-Origin: {$siteUrl}");
}
/**
* Permet d'obtenir une variable GET et nettoyé.
*
* @param string $name
* @return mixed
*/
public static function get( string $name ): mixed {
if( !isset( $_GET[$name] ) ) {
return null;
}
return self::sanitize( $_GET[$name] );
}
/**
* Permet d'obtenir une variable POST et nettoyé.
*
* @param string $name
* @return mixed
*/
public static function post( string $name ): mixed {
if( !isset( $_POST[$name] ) ) {
return null;
}
return self::sanitize( $_POST[$name] );
}
}

View File

@@ -192,4 +192,8 @@ final class Router {
return Kernel::$configs['general']['website_url'];
}
public static function getAssetURL( string $assetPath ): string {
return Kernel::$configs['general']['website_url'] . 'assets/' . $assetPath;
}
}