From e33c245d910e55b8cab407a03e669470509a705d Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Fri, 10 Mar 2017 21:46:12 +0100 Subject: Several changes have been made in this commit, which together with the previous commits result in version 1.1: + The rules for the Apache and nginx configuration have been changed and redirects now all requests to the index.php. + A router class has been added which now handles all requests that arrives at the application on the index.php. + Short-hand functions "PAGE", "POST" and "USER" for use in templates added to get specific item data by ID. + More language variables have been added to the core language. --- core/namespace/Router.php | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 core/namespace/Router.php (limited to 'core/namespace/Router.php') diff --git a/core/namespace/Router.php b/core/namespace/Router.php new file mode 100644 index 0000000..22784c2 --- /dev/null +++ b/core/namespace/Router.php @@ -0,0 +1,64 @@ + 'route', + 'pattern' => $pattern, + 'callback' => $callback + ]; + } + + #=============================================================================== + # Add redirect + #=============================================================================== + public static function addRedirect($pattern, $location, $code = 302) { + $pattern = Application::get('PATHINFO.BASE').$pattern; + + return self::$routes[] = [ + 'type' => 'redirect', + 'code' => $code, + 'pattern' => $pattern, + 'location' => $location + ]; + } + + #=============================================================================== + # Execute routing + #=============================================================================== + public static function execute($path) { + $path = ltrim($path, '/'); + $route_found = FALSE; + + foreach(self::$routes as $route) { + if($route['type'] === 'redirect') { + $location = preg_replace("#^{$route['pattern']}$#", $route['location'], $path, -1, $count); + + if($count) { + HTTP::redirect($location, $route['code']); + } + } + + else { + if(preg_match("#^{$route['pattern']}$#", $path, $matches)) { + # Remove the first element from matches which contains the whole string. + array_shift($matches); + + $route_found = TRUE; + call_user_func_array($route['callback'], $matches); + } + } + } + + if($route_found === FALSE) { + require_once 'system/404.php'; + } + } +} +?> \ No newline at end of file -- cgit v1.2.3