← Back
mainapp/Router.php
<?php
class Router {
  private array $routes = [];

  public function get(string $path, callable $handler): void { $this->routes['GET'][$path] = $handler; }
  public function post(string $path, callable $handler): void { $this->routes['POST'][$path] = $handler; }

  public function dispatch(string $path, string $method): void {
    $handler = $this->routes[$method][$path] ?? null;
    if (!$handler) {
      http_response_code(404);
      echo "404 Not Found";
      return;
    }
    $handler();
  }
}