summaryrefslogtreecommitdiff
path: root/lib/router.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/router.php')
-rw-r--r--lib/router.php46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/router.php b/lib/router.php
new file mode 100644
index 00000000..979968d4
--- /dev/null
+++ b/lib/router.php
@@ -0,0 +1,46 @@
+<?php
+
+class Router
+{
+ private $controller = '';
+ private $action = '';
+
+ public function __construct($controller = '', $action = '')
+ {
+ $this->controller = empty($_GET['controller']) ? $controller : $_GET['controller'];
+ $this->action = empty($_GET['action']) ? $controller : $_GET['action'];
+ }
+
+ public function sanitize($value, $default_value)
+ {
+ return ! ctype_alpha($value) || empty($value) ? $default_value : strtolower($value);
+ }
+
+ public function loadController($filename, $class, $method)
+ {
+ if (file_exists($filename)) {
+
+ require $filename;
+
+ if (! method_exists($class, $method)) return false;
+
+ $instance = new $class;
+ $instance->beforeAction($this->controller, $this->action);
+ $instance->$method();
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public function execute()
+ {
+ $this->controller = $this->sanitize($this->controller, 'app');
+ $this->action = $this->sanitize($this->action, 'index');
+
+ if (! $this->loadController('controllers/'.$this->controller.'.php', '\Controller\\'.$this->controller, $this->action)) {
+ die('Page not found!');
+ }
+ }
+}