order allow,deny deny from all RewriteEngine on # Don't use rewrite if its a real file or folder RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # In addition to the above rules, also ignore the index.php # file, anything in the assets folder and the robots.txt file RewriteCond $1 !^(index\.php|assets|robots\.txt) # Route everything else through the index.php file. RewriteRule ^(.*)$ index.php?/$1 [L] */ ob_start(); class Route { public static $params = []; public static $iParams = []; public static function param($name){ return self::$params[$name]; } public static function get($path, $file){ if($_SERVER['REQUEST_METHOD'] == 'GET'){ self::check($path, $file); } } public static function post($path, $file){ if($_SERVER['REQUEST_METHOD'] == 'POST'){ self::check($path, $file); } } public static function put($path, $file){ if($_SERVER['REQUEST_METHOD'] == 'PUT'){ self::check($path, $file); } } public static function delete($path, $file){ if($_SERVER['REQUEST_METHOD'] == 'DELETE'){ self::check($path, $file); } } private static function check($path, $file){ # use QUERY_STRING or default to '' $server_path = $_SERVER['QUERY_STRING'] ?: '/'; # break up $server_path into segments $server_path = explode('/', $server_path); # break up $path into segments $path = explode('/', $path); # identify a match # if both $server_path and $path have the same amount of segments if(count($server_path) == count($path)){ $match = true; # then check if they have the same segments foreach($server_path as $key => $sp_segment){ # check if this one is a param if(substr($path[$key], 0, 1) == ':'){ # if it is, then add it to $params self::$params[substr($path[$key], 1)] = $sp_segment; self::$iParams[] = $sp_segment; }else{ # if not, then check if the segments are the same if($server_path[$key] != $path[$key]){ $match = false; } } } if($match){ if(strpos($file, '->') !== false){ $path = explode('->', $file); // constructs an instance of the controller class $c = new $path[0](); // run the function in the controller call_user_func_array([$c, $path[1]], self::$iParams); ob_end_flush(); // can't believe I forgot to do this. exit; } else { # then require the file ob_end_flush(); require_once $file; exit; } } } } public static function fallback($file){ http_response_code(404); require_once $file; exit; } }