| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/c.pacim.de/web/app/ |
Upload File : |
<?php
declare(strict_types=1);
namespace App;
final class Router
{
private array $routes = ['GET' => [], 'POST' => []];
public function get(string $path, callable|array $handler): void
{
$this->routes['GET'][$path] = $handler;
}
public function post(string $path, callable|array $handler): void
{
$this->routes['POST'][$path] = $handler;
}
public function dispatch(string $method, string $uri): mixed
{
$path = parse_url($uri, PHP_URL_PATH) ?: '/';
$basePath = rtrim((string) Config::get('app.base_path', ''), '/');
if ($basePath !== '' && str_starts_with($path, $basePath)) {
$path = substr($path, strlen($basePath)) ?: '/';
}
<<<<<<< HEAD
if ($path !== '/') {
$path = rtrim($path, '/');
}
$routesByMethod = $this->routes[$method] ?? [];
$handler = $routesByMethod[$path] ?? null;
=======
$handler = $this->routes[$method][$path] ?? null;
>>>>>>> origin/main
if (!$handler) {
http_response_code(404);
echo '404 Not Found';
return null;
}
if (is_array($handler)) {
[$class, $action] = $handler;
$instance = new $class();
return $instance->{$action}();
}
return $handler();
}
}