$options) { // Check if path matches the route if(preg_match($regex, $path, $matches)) { // Get controller $controller = $options["controller"]; // Get route type $route_type = $options["type"] ?? "page"; $require_log_in = $options["require_log_in"] ?? false; $require_admin = $options["require_admin"] ?? false; $require_user_level = $options["require_user_level"] ?? false; $allow_ajax = $options["allow_ajax"] ?? true; // Get params $params = $options["params"] ?? []; // Set param variables if($params) { foreach($params as $index => $name) { ${$name} = $matches[$index + 1]; } } $controller_directory = "controllers"; $controller_path = $controller_directory . "/" . $controller . ".php"; if(!file_exists($controller_path)) throw new Exception("Controller \"$controller\" not found."); if($require_log_in) { Core::requireLogIn(); } if($require_admin) { Core::requireAdmin(); } if($require_user_level) { Core::requireUserLevel($require_user_level); } if(!$allow_ajax) { Core::refuseAJAX(); } // Load the controller require $controller_path; // Session cleanup if(empty($_SESSION) && session_status() == PHP_SESSION_ACTIVE) { session_destroy(); } // Quit exit; } } // 404 if no page was found require "controllers/404.php"; } catch (Exception $e) { $route_type = $route_type ?? "page"; // Force JSON exceptions for AJAX calls if(HTTP::isAJAX()) { $route_type = "json"; } if($route_type == "page") { // Show error page $page_title = "Error"; View::includeOnce("header", compact("page_title")); View::include("error", ["error_message" => $e->getMessage()]); View::includeOnce("footer"); } else if($route_type == "json") { // JSON error JSON::header(); JSON::fail(["reason" => $e->getMessage()]); } else { echo $e->getMessage(); } } ?>