class Router
{
private $routes = [];
public function add($method, $path, $handler)
{
$this->routes[] = [
'method' => $method,
'path' => $path,
'handler' => $handler
];
}
public function dispatch($request)
{
foreach ($this->routes as $route) {
if ($route['method'] === $request->method &&
preg_match($route['path'], $request->path, $matches)) {
return call_user_func_array($route['handler'], $matches);
}
}
throw new Exception("Route not found");
}
}