feat: front controller, routage et gestion d'erreurs
This commit is contained in:
1 parent
a7003886da
commit
e8a9deb7ad
2 files changed
+37
No files matched your search
@@ -0,0 +1,4 @@
|
||||
RewriteEngine On
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteRule ^ index.php [QSA,L]
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../src/Database.php';
|
||||
require_once __DIR__ . '/../src/LinkRepository.php';
|
||||
require_once __DIR__ . '/../src/JsonExporter.php';
|
||||
require_once __DIR__ . '/../src/LinkController.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(204);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo = Database::connect();
|
||||
|
||||
$controller = new LinkController(
|
||||
new LinkRepository($pdo),
|
||||
new JsonExporter($pdo, dirname(__DIR__) . '/data.json'),
|
||||
);
|
||||
|
||||
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
$controller->handle($_SERVER['REQUEST_METHOD'], $path);
|
||||
} catch (Throwable $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Erreur interne du serveur.'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
Reference in new issue
Block a user