0) $strParam = $_SERVER["PATH_INFO"]; } $strPath = $strParam; $strPath = preg_replace("/^\//", "", $strPath); $strPath = preg_replace("/\/$/", "", $strPath); // /test/action/a/b/c $fncIsFile = function($strScriptPath) { $varSearchPaths = []; if (is_dir("plugins")) foreach (scandir("plugins") as $strPluginName) { if ($strPluginName == ".") continue; if ($strPluginName == "..") continue; $strNewPath = "plugins/{$strPluginName}"; if (is_dir($strNewPath)) $varSearchPaths[] = "{$strNewPath}"; } // Try the framework's directory last: $varSearchPaths[] = "."; foreach ($varSearchPaths as $strSearchPath) if (is_file(Request::$strScriptPath = "{$strSearchPath}/{$strScriptPath}")) return true; Request::$strScriptPath = null; return false; }; while (true) { if (strlen($strPath) < 1) if ($fncIsFile("pages/index.php")) break; if ($fncIsFile("pages/{$strPath}/index.php")) break; if ($fncIsFile("pages/{$strPath}.php")) break; if (preg_match("/\//", $strPath)) $strPath = preg_replace("/\/[^\/]{1,}$/", "", $strPath); else $strPath = ""; } $strArgs = str_replace("/{$strPath}", "", $strParam); $varArgs = explode("/", $strArgs); array_shift($varArgs); Request::$varArgs = $varArgs; Request::$strResourcePath = $strPath; } public static function getScript() { return Request::$strScriptPath; } public static function getArgs() { return Request::$varArgs; } // Safely returns a request argument by its index or null if it doesn't exist (without error) public static function getArg($intIndex) { if (count(Request::$varArgs) >= $intIndex + 1) return Request::$varArgs[$intIndex]; else return null; } // Safely returns the value of a request parameter or null if not defined (without error) // e.g. 12345 from getParam("id") when resource is like /users/get?id=12345 public static function getParam($strKey) { if (is_array($_GET)) if (array_key_exists($strKey, $_GET)) if (strlen($_GET[$strKey]) > 0) return $_GET[$strKey]; return null; } // Returns true if all of the arguments are in the POST request // To be used like: // if (Request::posts("param1", "param2")) { public static function posts() { if (is_array($_POST)) if (func_num_args() > 0) { foreach (func_get_args() as $strKey) if (!array_key_exists($strKey, $_POST)) return false; return true; } return false; } // Returns the value of the posted field public static function getPosted($strKey) { if (is_array($_POST)) if (array_key_exists($strKey, $_POST)) if (strlen($_POST[$strKey]) > 0) return $_POST[$strKey]; return null; } } // Call this no matter what to understand the request: Request::process(); ?>