diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:16:23 +0200 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:16:23 +0200 |
| commit | 7076343338ae3439f3c86f01144818abe8c31978 (patch) | |
| tree | 794abb1e6b8f821091fd095341885496b6dad51d /classes/Route.php | |
| parent | 47cbb529f5723b246125ae083a193e11481b89ef (diff) | |
| download | classroom-7076343338ae3439f3c86f01144818abe8c31978.tar.gz classroom-7076343338ae3439f3c86f01144818abe8c31978.tar.bz2 classroom-7076343338ae3439f3c86f01144818abe8c31978.zip | |
add container helpers; constuct public directory-tree
Diffstat (limited to 'classes/Route.php')
| -rw-r--r-- | classes/Route.php | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/classes/Route.php b/classes/Route.php deleted file mode 100644 index 072723c..0000000 --- a/classes/Route.php +++ /dev/null @@ -1,97 +0,0 @@ -<?php - -/** Route - * --- - * Methods: - * - add( expression , callback_function , method ) - * - notFound( callback_function ) - * - methodNotAllowed( callback_function ) - * - run() - * - * + matches static paths - * + matches dynamic paths through regex expressions - * + takes care of request-method - */ -class Route { - - private static $routes = Array(); // array to host routes - private static $notFound = null; // 404 error function - private static $methodNotAllowed = null; // 404 error function - - /** add (route) - * --- - * @param $expression : (string) static or regex matcher - * @param $function : callback function to be executed if expression is matched - * @param $method : get / post / any - */ - public static function add( $expression, $function, $method = 'get' ) - { - array_push(self::$routes, Array( - 'expression' => $expression, - 'function' => $function, - 'method' => strtolower($method) - )); - } - - - /** notFound($function) - * --- - * @param $function : call back function to be executed - */ - public static function notFound($function) - { - self::$notFound = $function; - } - - - /** run() - * --- - * Parse request ; Find mathing route ; - * then call route's function - * usualy a Controller::method([poarametres]) - */ - public static function run(Request $request) - { - // $request = Registry::get('REQUEST'); - $path = $request->PATH; // request path - $method = $request->METHOD; // request method - - $path_match_found = false; - $route_match_found = false; - - foreach(self::$routes as $route) { - - // If method matched check the path - if ($route['method'] == $method || $method == 'any') { - - // Add 'find string start' automatically - $route['expression'] = '^'.$route['expression']; - - // Add 'find string end' automatically - $route['expression'] = $route['expression'].'$'; - - // Check path match - if (preg_match('#'. $route['expression'] .'#', $path, $matches)) { - - $route_match_found = true; - - array_shift($matches); // Always remove first element. This contains the whole string - - call_user_func_array($route['function'], $matches); - break; // Do not check other routes - - } - } - } - - // No matching route was found - if (!$route_match_found) { - header("HTTP/1.0 404 Not Found"); - if (self::$notFound) { - call_user_func_array(self::$notFound, []); - } - } - - } - -} |
