summaryrefslogtreecommitdiffstats
path: root/core/namespace
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2017-03-10 21:46:12 +0100
committerThomas Lange <code@nerdmind.de>2017-03-10 21:46:12 +0100
commite33c245d910e55b8cab407a03e669470509a705d (patch)
treee958504564ab47e72e0d3dcfe0b967440007b1d9 /core/namespace
parentaae885b9784466ab412e4010893808867e93c213 (diff)
downloadblog-e33c245d910e55b8cab407a03e669470509a705d.tar.gz
blog-e33c245d910e55b8cab407a03e669470509a705d.tar.xz
blog-e33c245d910e55b8cab407a03e669470509a705d.zip
Several changes have been made in this commit, which together with the previous commits result in version 1.1:v1.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.
Diffstat (limited to 'core/namespace')
-rw-r--r--core/namespace/Router.php64
1 files changed, 64 insertions, 0 deletions
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 @@
+<?php
+class Router {
+ private static $routes = [];
+
+ #===============================================================================
+ # Add route
+ #===============================================================================
+ public static function add($pattern, callable $callback) {
+ $pattern = Application::get('PATHINFO.BASE').$pattern;
+
+ return self::$routes[] = [
+ 'type' => '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