Files
project16/Router.php
Sapoleone cfaafcffa6 Add initial project structure and configurations
- Set up project directories including app structure.
- Add IntelliJ IDEA project files and configurations.
- Implement basic PHP router for request handling.
- Provide reference files for features, IPs, and documentation.
- Include license and copyright files.
2025-09-23 23:56:22 +02:00

48 lines
1.3 KiB
PHP

<?php
/*
* Copyright (c) 2025. Brusegan Samuele, Davanzo Andrea
* Questo file fa parte di VeyonCtrl ed è rilasciato
* sotto la licenza MIT. Vedere il file LICENSE per i dettagli.
*/
class Router {
private array $routes = [];
// Aggiunge una nuova rotta
public function add($url, $controller, $action): void {
$this->routes[$url] = [
'controller' => $controller,
'action' => $action
];
}
// Smista la richiesta all'azione corretta
public function dispatch($url): void {
$path = rtrim(parse_url($url, PHP_URL_PATH), '/');
if ($path === '') {
$path = '/';
}
if (array_key_exists($path, $this->routes)) {
$route = $this->routes[$path];
$controllerName = $route['controller'];
$actionName = $route['action'];
// Crea il controller e chiama l'azione
$controller = new $controllerName();
$controller->$actionName();
} else {
// Gestione errore 404
header("HTTP/1.0 404 Not Found");
echo "<pre>";
echo "Pagina non trovata!";
echo $path; echo "<br>";
echo $url; echo "<br>";
echo PHP_URL_PATH; echo "<br>";
echo "</pre>";
}
}
}